Jump to content

Errors when updating data in MySQL


Seaholme

Recommended Posts

Hey all,

 

I've been trying to get a script to run which will allow me to use PHP to update fields in the database. I found http://www.phpeasystep.com/mysql/9.html this website which explains how to do it, and followed all the steps. Every one of them has worked except for my attempt at the final step (Create file update_ac.php) which throws up the message it gives when the whole thing has been unsuccessful (ERROR).

 

Can anybody help me work out what it is about it that is causing the error to be thrown up? Also: could it be in one of the previous pages?

 

<?php
$dbhost = 'xxxxx';
$dbuser = 'xxxx
$dbpass = 'xxxxx
$dbname = 'xxxxx';
$tbl_name="players"; // Table name

// Connect to server and select database.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE players SET displayname='$displayname', campname='$campname', WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

 

Thanks!

Link to comment
Share on other sites

I don`t see anywhere in your code that $displayname $campname and $id are picked up on for the system. You would need to add in

$displayname = $_GET['displayname'];

$campname = $_GET['campname'];

$id = $_GET['id'];

 

If you`re using post method for the form on the previous page, then replace GET with POST. Without specifying those before you tell the system to add them in, the system won`t know what it`s supposed to grab.

Link to comment
Share on other sites

Thanks! I changed it as you suggested, but it still throws up an error. This is the edited version...

 

<?php
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxxx';
$tbl_name="players"; // Table name

// Connect to server and select database.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");

$displayname = $_POST['displayname'];
$campname = $_POST['campname'];
$id = $_POST['id'];

// update data in mysql database 
$sql="UPDATE players SET displayname = '$displayname', campname = '$campname', WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

 

EDIT: I also thought I'd add in the php for the previous page, in case that helps

 

<?php
$dbhost = 'xxx';
$dbuser = 'xxxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$tbl_name="players"; // Table name

// Connect to server and select database.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Camp Name</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="displayname" type="text" id="displayname" value="<? echo $rows['displayname']; ?>"></td>
<td align="center"><input name="campname" type="text" id="campname" value="<? echo $rows['campname']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection 
mysql_close();

?>

Link to comment
Share on other sites

 

EDIT: You were posting the form as I was typing this out, apparently . . .

Need to see more of the code, and the form to get a better idea of what's going on. Also, according to the website, the `id` field in the database table is of the integer type, so you should remove the single quotes around the variable in the WHERE clause:  WHERE id='$id'", and the variable $id should be cast as an integer as well.

Link to comment
Share on other sites

Ok, this line:

$sql="UPDATE players SET displayname = '$displayname', campname = '$campname', WHERE id='$id'";

 

Change it to:

$sql="UPDATE players SET displayname = '$displayname', campname = '$campname' WHERE id='$id'";

 

You have an extra comma that shouldn`t be there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.