geroido Posted August 18, 2008 Share Posted August 18, 2008 Hi I've been working on this since yesterday. I'm trying to see if a record exists in a database. If it does then update it. If it doesn't then insert a new record. I first check to see if there is a value in the variable '$seats' posted from the page. If there is no value, I return the form. If there is a value, I check the database for a matcing record. If I get a match, I update. This all works fine now. However, if I don't get a match then the record doesn't exist so I want to insert it. This part won't work. I know it's something to do with the 'else' statement and curly brackets({}) but I can't find it. I've tried every combination of brackets and keep getting an error. Any help? Parse error: syntax error, unexpected $end in C:\xampp\htdocs\pastelco\updateseats.php on line 151 <? if (!is_numeric($seats)) { ?> <h3>Please complete <EM>ALL</EM> of the fields below.</h3> <form method="post" name="addseats" action="updateseats.php"> <TABLE> <TR><TD><H3>Please enter the number of seats in your restaurant</H3></TD></TR> <TR><TD><textarea rows="5" cols="40" name="seats" value=<?echo $seats?>> </textarea></TD></TR> <TR></TABLE><TABLE> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Save" /></TD> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Finish" /></TD></TR> </TABLE> <? } elseif (is_numeric($seats)) { $query = "select clientID, NumSeats from $table where clientID = '" .$_SESSION['userid']. "'"; $results = mysql_query($query, $link) or die("Sorry, but you have no orders for today"); if(mysql_num_rows($results) > 0){ $query = "update $table set NumSeats = $seats where clientID = '".$_SESSION['userid']."'"; $results = mysql_query($query, $link) or die("Sorry, but you have no orders for today"); ?> <H3>Your restaurant seating capacity has been edited to the following: </h3> <TABLE border="2"><TR><TD>Number of seats</TD></TR> <TR><TD><?print $seats?></TD></TR></TABLE> <BR> <? } } else{ $insert = mysql_query("insert into $table (ClientID, NumSeats) values ('" .$_SESSION['userid']. "', '".$seats."')") or die("Could not insert data because ".mysql_error());?> } Link to comment https://forums.phpfreaks.com/topic/120183-update-or-insert-on-same-page-problem/ Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 Why don't you use INSERT with the ON DUPLICATE KEY UPDATE ? http://dev.mysql.com/doc/refman/5.0/en/insert.html Link to comment https://forums.phpfreaks.com/topic/120183-update-or-insert-on-same-page-problem/#findComment-619142 Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 On your last line, you close PHP before your bracket $insert = mysql_query("insert into $table (ClientID, NumSeats) values ('" .$_SESSION['userid']. "', '".$seats."')") or die("Could not insert data because ".mysql_error());?> } Change to $insert = mysql_query("insert into $table (ClientID, NumSeats) values ('" .$_SESSION['userid']. "', '".$seats."')") or die("Could not insert data because ".mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/120183-update-or-insert-on-same-page-problem/#findComment-619148 Share on other sites More sharing options...
geroido Posted August 18, 2008 Author Share Posted August 18, 2008 I've altered that and no more error. However it's still not going to the final else to carry out the insert. Everything else is working ok. Any ideas? <? if (!is_numeric($seats)) { ?> <h3>Please complete <EM>ALL</EM> of the fields below.</h3> <form method="post" name="addseats" action="updateseats.php"> <TABLE> <TR><TD><H3>Please enter the number of seats in your restaurant</H3></TD></TR> <TR><TD><textarea rows="5" cols="40" name="seats" value=<?echo $seats?>> </textarea></TD></TR> <TR></TABLE><TABLE> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Save" /></TD> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Finish" /></TD></TR> </TABLE> <? } elseif (is_numeric($seats)) { $query = "select clientID, NumSeats from $table where clientID = '" .$_SESSION['userid']. "'"; $results = mysql_query($query, $link) or die("Sorry, but you have no orders for today"); if(mysql_num_rows($results) > 0){ $query = "update $table set NumSeats = $seats where clientID = '".$_SESSION['userid']."'"; $results = mysql_query($query, $link) or die("Sorry, but you have no orders for today"); ?> <H3>Your restaurant seating capacity has been edited to the following: </h3> <TABLE border="2"><TR><TD>Number of seats</TD></TR> <TR><TD><?print $seats?></TD></TR></TABLE> <BR> <? } } else{ echo ("We've arrived here anyway"); $insert = mysql_query("insert into $table (ClientID, NumSeats) values ('" .$_SESSION['userid']. "', '".$seats."')") or die("Could not insert data because ".mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/120183-update-or-insert-on-same-page-problem/#findComment-619328 Share on other sites More sharing options...
geroido Posted August 18, 2008 Author Share Posted August 18, 2008 Hi revraz Unbelievable. I didn't know anything about the ON DUPLICATE statement. Just tried it and it works a treat. So much easier. Brilliant. Thanks Link to comment https://forums.phpfreaks.com/topic/120183-update-or-insert-on-same-page-problem/#findComment-619341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.