TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   PHP update script (http://www.talkphp.com/mysql-databases/6089-php-update-script.html)

ChrisGuru 01-10-2012 12:55 PM

PHP update script
 
Having trouble with this upload script, it grabs and displays the content from the database fine. It won't however update the database although the code is throwing up no errors;
this is the script that displays the content

PHP Code:

############### Code

<?php
include("includes/connection.php");

$sql="SELECT * FROM mot";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['header']; ?></td>
<td><? echo $rows['content']; ?></td>

// link to update.php and send value of id 
<td align="center"><a href="update_content.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close
();
?>

this next bit of code is the code that user updates the content;

PHP Code:

<?php 

include("includes/connection.php");

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


// Retrieve data from database 
$sql="SELECT * FROM mot 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>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['header']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['content']; ?>" size="15"></td>
</tr>
<tr>
<td>&nbsp;</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>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection 
mysql_close();

?>

this final bit is the update code for the database which isnt working but not throwing up any errors

PHP Code:

<?php
 
include("includes/connection.php");

// If form button has been pressed then do the following 
if(isset($_POST['update'])){ 
// Get id of post 
$id $_GET['id'];

$header $_POST['header'];
$content $_POST['content'];
 
// update data in mysql database 
$sql="UPDATE mot SET header = '$header', content = '$content' WHERE id='$id'";
$result=mysql_query($sql);

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

else {
echo 
"ERROR";
}
}
?>

Any help would be much appreciated

tony 01-10-2012 09:57 PM

Does it Print ERROR at least from your if($result)? can you test that the sql statement runs on your mysql database and works right? is the id field a string or a number? because you are updating is a a string/date/enum.

Just trying to figure out if it is a database problem or programming problem. The script at first glance looks good.

maeltar 01-11-2012 07:39 AM

Try :

Code:


if(!$result){
echo "Error with update : " . mysql_error();
die();
}else{
echo "Successful";
echo "<BR>";
echo "<a href='mot_edit.php'> View result</a>";

}
}


maeltar 01-11-2012 07:40 AM

Also maybe worth using an echo on your sql statement, copy and paste it into your MySQL console see what error that kicks out..


All times are GMT. The time now is 02:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0