Log in

View Full Version : help using FOR then adding to data-base



Timms
02-28-2013, 11:05 PM
My form consists of a "select amount of people" select menu,
a select menu to select the hour,
and a date select menu,

What is meant to happen:
the user selects how many people is meant to be booked in and which hour of the day they are booking in, each person gets booked in for 1 hour each,
so if they were booking in 3 people that is 3 hours they are booking in for, from the hour of the day selected, so if someone books in 3 people at 3pm that books in from 3 tell 6 so (3pm 4pm 5pm)

so using a for statement i grab these times to prepare them to be added to the mysql data-base to make these times booked so no one else can book these times on this date. So how would i add these to a mysql data-base using a for statement?

here is my code:
compdate is the day of the month then the month completed how it should be,
i made the hour -1 so when it adds it adds the correct time because when it runs the for tag its always +1

$compdate = $listday . "/" . $listmonth;
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$result=mysql_query($sql);

$listhour = $listhour-1;
for($i = 0; $i > $people; $i++)
{
//echo $i+1 . " ";
$listhour = $listhour+1;
$newtimes = $listhour . " " . $listampm;

$sql="INSERT INTO $tbl_name(dates, times)VALUES('$compdate', '$newtimes')";
if($result){
echo "$newtimes Success!";
}

else {
echo "$newtimes ERROR";
}

mysql_close();
}
//check if query successful


Obviously this looks like it would work but dosent because it wont run the mysql correctly and insert it to the data-base, a bit of sloppy code which i need help, so it adds all the times to the mysql that have been booked in.

Apologies if this is a MySQL question i couldn't figure out if it was a MySQL rather than a php because it uses the for tag i leaned more to that php side of things or at least that's how it seems to me :eek:

Help is appreciated im really stuck on this!

Thanks

traq
03-01-2013, 06:47 AM
You've got things out-of-order. You're trying to query the DB before you create your $sql.

(Incidentally, this means that you should be seeing an error. Make sure you have error reporting enabled if you don't.

(There are lots of undefined variables in the code you posted. I'm going to assume that most of them are defined elsewhere, but you should definitely double-check. When posting code excerpts like this, it's always a good idea to let people know where you did indeed define a var somewhere else, so no one wastes time suggesting you fix something that might not be broken.)

You should not use the mysql_* functions. They are deprecated. You should look into mysqli (http://php.net/mysqli) or PDO (http://php.net/pdo).

I'm not sure what you're trying to do with $listhour. If it's just the initial iteration of the loop that you're trying to fix, simply increment it after you write your sql:
for($i = 0; $i > $people; $i++){
// $listhour = $listhour+1;
$newtimes = $listhour . " " . $listampm;
$listhour = $listhour+1;
// etc. . .

Speaking of your loop, it's always best not to do multiple queries inside loops. Instead, write your query and execute it only once:
$sql = "INSERT INTO `tbl_name`( `col_1`,`col_2` ) ";
for( $i=0; $i < $max; $i++ ){
$sql_values[] = "VALUES( '$val_1','$val_2' ) ";
}
# concatenate VALUE clauses and append to SQL query
$sql .= implode( ',',$sql_values );
# *now* execute the query

Just from looking at the value you're preparing for your DB, I would suspect that there's a better way to design your DB table. That's a separate issue, of course, but if you're interested you can post your DB schema.

For any further help, we'd probably need to see more of your code.

Timms
03-01-2013, 11:55 AM
really appriciate the answer you gave me.

When i left the forum after i posted i went to look in some more posibilities to what i was doing, and figured maybe the do command would be a better option for something like this?

here was my alternative and it seems to work perfectly, though looking at your post i would still need to make the changes either way (depriciated tags ) etc Thank you for helping


//pull out the information we need from user selection
$name = $_POST ['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$address_1 = $_POST['address_1'];
$address_2 = $_POST['address_2'];
$address_3 = $_POST['address_3'];
$postcode = $_POST['postcode'];
$people = $_POST['people'];
$listday = $_POST['listday'];
$listmonth = $_POST['listmonth'];
$listhour = $_POST['listhour'];
$listampm = $_POST['listampm'];
//build the complete date and put the year on (for future use)
$compdate = $listday . "/" . $listmonth . "/" . "2013";
//set listhour to -1 otherwise it will start 1 hour late because of the +1 in loop this will set it correct when processed
$listhour = $listhour-1;
//set our $i to the correct value before we do anything
$i = 1;
//now lets process!
do
{
//if the hour is now 12 o clock the next one will be 13 o clock so reset it to 0 if its 12 and then the next process will put it to 1
//we also need to amend the am or pm
//if we are at 12 am
if ($newtimes == 12 . " AM") {
//reset the hour to 0
$listhour = 0;
//change am to pm
$listampm = "PM";
//test
echo "<br />AM CHANGED TO PM<br />";
//12 pm
}elseif ($newtimes == 12 . " PM") {
//rest hour
$listhour = 0;
//change pm to am
$listampm = "AM";
//test
echo "<br />PM CHANGED TO AM<br />";
}
//set listhour to +1 (this is where it gets corrected
$listhour++;
//set i to +1 so we can count up to how many people
$i++;
//update newtimes with the new hour and add am/pm to the end of it
$newtimes = $listhour . " " . $listampm;
//connect to data-base
$host="host"; // Host name
$username="user"; // Mysql username
$password="pass"; // Mysql password
$db_name="db name"; // Database name
$tbl_name="readings"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
//test before adding to data-base!
echo $i . " = " . $compdate . " " . $newtimes . "<br />";
//drop the query in to the data-base with the values
$sql="INSERT INTO $tbl_name(dates, times)VALUES('$compdate', '$newtimes')";
//now lets pull out the results
$result=mysql_query($sql);
//now test to see if it was put in or if it failed
if($result){
echo "$compdate $newtimes Success!<br />";
}

else {
echo "$compdate $newtimes ERROR<br />";
}
}
//keep looping tell we have added the correct amount = people from user selection (less than or equal to then repeat)
while ($i<=$people);
//close the connection
mysql_close();