Jump to content

Update or insert on same page problem


geroido

Recommended Posts

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

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());

}

?>

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());
  
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.