/    Sign up×
Community /Pin to ProfileBookmark

Can’t update sql row with my php code

So I am trying to edit a guestbook on a wedding site. I have a page that shows all of my guests who have registered for the wedding. Next to each row I have an edit and delete button. The delete button works, but the entire edit process does not. When I click edit, I then go to the edit.php page, see below:

[code=php]<?php
session_start();
if (@$_SESSION[‘auth’] !=”yes”)
{
header(“Location: login_form.php”);
exit();
}
?>
<?php
$host=”localhost”;
$username=”username”;
$password=”password”;
$db_name=”database”;

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

$id = $_GET[‘id’];
$results = mysql_query(“SELECT * FROM guests WHERE id = ‘$id'”);
$rows = mysql_fetch_assoc($results);

?>
<html>
<head>
<link href=”style.css” type=”text/css” rel=”stylesheet”>
</head>
<body>

<div align=”center”>
<form action=”edit_process.php” method=”post”>
<input type=”hidden” name=”id” value=”<?php echo $row[‘id’]; ?>”>
<table width=”70%” cellpadding=”0″ cellspacing=”0″ border=”0″>
<tr>
<td width=”30%”><table width=”100%” cellpadding=”0″ cellspacing=”0″ border=”0″>
<tr>
<td>Guest 1 <input type=”text” value=”<? echo $rows[‘guest_1’]; ?>” name=”guest_1″></td>
</tr>
<tr>
<td>Guest 2 <input type=”text” value=”<? echo $rows[‘guest_2’]; ?>” name=”guest_2″></td>
</tr>
<tr>
<td>Guest 3 <input type=”text” value=”<? echo $rows[‘guest_3’]; ?>” name=”guest_3″></td>
</tr>
<tr>
<td>Guest 4 <input type=”text” value=”<? echo $rows[‘guest_4’]; ?>” name=”guest_4″></td>
</tr>
<tr>
<td>Guest 5 <input type=”text” value=”<? echo $rows[‘guest_5’]; ?>” name=”guest_5″></td>
</tr>
</table></td>

<td width=”70%”><table width=”100%” cellpadding=”0″ cellspacing=”0″ border=”0″>
<tr>
<td>Will Be Attending <input type=”text” value=”<? echo $rows[‘attending’]; ?>” name=”vegetarian”></td>
</tr>
<tr>
<td>Number Vegetarian Meals <input type=”text” value=”<? echo $rows[‘vegetarian’]; ?>” name=”vegetarian”></td>
</tr>
<tr>
<td>How Many People Attending<input type=”text” value=”<? echo $rows[‘total_guests’]; ?>” name=”vegetarian”></td>
</tr>
<tr>
<td>Email Address <input type=”text” value=”<? echo $rows[’email’]; ?>” name=”vegetarian”></td>
</tr>
</table></td>
</tr>
<tr>
<td><input type=”submit” value=”Update”></td>
</tr>
</table>
</form>
</div>
</body>
</html>[/code]

In the URL bar it displays the correct id and all of the information pops up in the correct boxes. Here is where it doesn’t work. When I click update, it doesn’t update on the sql server. Here is the code for the edit_process.php :

[code=php]<?php

$host=”localhost”;
$username=”username”;
$password=”password”;
$db_name=”database”;

header(“Location: rsvp_view.php”);

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

$id = $_POST[“id”];
$guest_1 = $_POST[‘guest_1’];
$guest_2 = $_POST[‘guest_2’];
$guest_3 = $_POST[‘guest_3’];
$guest_4 = $_POST[‘guest_4’];
$guest_5 = $_POST[‘guest_5’];
$attending = $_POST[‘attending’];
$vegetarian = $_POST[‘vegetarian’];
$total_guests = $_POST[‘total_guests’];
$email = $_POST[’email’];

$query = ‘SELECT * FROM $tbl_name WHERE id = $id’;

mysql_query(“UPDATE guests SET guest_1=’$guest_1′,guest_2=’$guest_2′,guest_3=’$guest_3′.guest_4=’$guest_4′,guest_5=’$guest_5′,attending=’$attending’,vegetarian=’$vegetarian’,total_guests=’$total_guests’,email=’email’ WHERE id=$id”);
echo mysql_error();
?>[/code]

Any ideas of why it isn’t working. This is the last piece of the puzzle for this site, so any help will be appreciated.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@Four_StaplesJun 09.2009 — [code=php]header("Location: rsvp_view.php"); [/code]
Is there a reason you're redirecting them before it even runs your script? ?
Copy linkTweet thisAlerts:
@themonkey40authorJun 09.2009 — no, I wasn't aware I am, could that be the problem?
Copy linkTweet thisAlerts:
@Four_StaplesJun 09.2009 — no, I wasn't aware I am, could that be the problem?[/QUOTE]

Yes. Move that header() down to the bottom of the file.
Copy linkTweet thisAlerts:
@themonkey40authorJun 09.2009 — I moved the header, see below:
[code=php]<?php

$host="localhost";
$username="username";
$password="password";
$db_name="database";
$tbl_name="table";

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

$id = $_POST["id"];
$guest_1 = $_POST['guest_1'];
$guest_2 = $_POST['guest_2'];
$guest_3 = $_POST['guest_3'];
$guest_4 = $_POST['guest_4'];
$guest_5 = $_POST['guest_5'];
$attending = $_POST['attending'];
$vegetarian = $_POST['vegetarian'];
$total_guests = $_POST['total_guests'];
$email = $_POST['email'];

$query = 'SELECT * FROM $tbl_name WHERE id = $id';

mysql_query("UPDATE guests SET guest_1='$guest_1',guest_2='$guest_2',guest_3='$guest_3',guest_4='$guest_4',guest_5='$guest_5',attending='$attending',vegetarian='$vegetarian',total_guests='$total_guests',email='email' WHERE id=$id");
echo mysql_error();

header("Location: rsvp_view.php");
?>[/code]


But now I get this error when the page processes: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Warning: Cannot modify header information - headers already sent by (output started at /home/content/m/a/r/markandmichell/html/manager/edit_process.php:27) in /home/content/m/a/r/markandmichell/html/manager/edit_process.php on line 29"

Any ideas
Copy linkTweet thisAlerts:
@Four_StaplesJun 09.2009 — Try changing your query to this...[code=php]mysql_query("UPDATE guests SET guest_1='{$guest_1}', guest_2='{$guest_2}', guest_3='{$guest_3}', guest_4='{$guest_4}', guest_5='{$guest_5}', attending='{$attending}', vegetarian='{$vegetarian}', total_guests='{$total_guests}', email='{$email}' WHERE id='{$id}'");[/code]
Copy linkTweet thisAlerts:
@themonkey40authorJun 09.2009 — Did that:
[code=php]// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$id = $_POST["id"];
$guest_1 = $_POST['guest_1'];
$guest_2 = $_POST['guest_2'];
$guest_3 = $_POST['guest_3'];
$guest_4 = $_POST['guest_4'];
$guest_5 = $_POST['guest_5'];
$attending = $_POST['attending'];
$vegetarian = $_POST['vegetarian'];
$total_guests = $_POST['total_guests'];
$email = $_POST['email'];

$query = 'SELECT * FROM $tbl_name WHERE id = $id';

mysql_query("UPDATE guests SET guest_1='{$guest_1}', guest_2='{$guest_2}', guest_3='{$guest_3}', guest_4='{$guest_4}', guest_5='{$guest_5}', attending='{$attending}', vegetarian='{$vegetarian}', total_guests='{$total_guests}', email='{$email}' WHERE id='{$id}'");
echo mysql_error();

header("Location: rsvp_view.php");[/code]


I no longer get the error, but the database still isn't updated. I can't think of anything else that is going wrong.
Copy linkTweet thisAlerts:
@Four_StaplesJun 09.2009 — Let's see if the ID is getting passed right. Put this at the top of the file:[code=php]exit($_POST['id']);[/code]
Copy linkTweet thisAlerts:
@themonkey40authorJun 09.2009 — When I put that at the top of the edit_process.php file, nothing happens. The page just goes white. Now something interesting I discovered. When I am on the rsvp page and click edit, the id of the row that I want to edit appears in the url "manager/edit.php?id=7" but when I make changes and click update, this is what appears in the url "manager/edit_process.php". The ID information all but disappears when it is trying to update the row.

I am not sure if you know of an easier way of accomplishing this, as you have seen my code. I am open to suggestions.
Copy linkTweet thisAlerts:
@Four_StaplesJun 10.2009 — When I put that at the top of the edit_process.php file, nothing happens.[/QUOTE]Ahh. Yeah, that's the point. It means your ID isn't being passed through in the hidden form variable-- if it were, you'd have seen it on that page. Most likely it's not being sent to edit_process.php at all.

EDIT: And yes, that's the problem. This line in your HTML:[code=html]<input type="hidden" name="id" value="<?php echo $row['id']; ?>"> [/code]
You have $row['id'] instead of $rows['id'].
Copy linkTweet thisAlerts:
@themonkey40authorJun 10.2009 — Man you are good. That one "s" is what was keeping this from working the whole time. A thousand thanks man, you really helped me out on this one.
×

Success!

Help @themonkey40 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...