Jump to content

PHP update script


superchrisc

Recommended Posts

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

############### 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>

 

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

 

<?php 

include("includes/connection.php");


$id=$_GET['id'];


$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> </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>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
</tr>
<tr>
<td> </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> </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>


 

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

 

<?php

include("includes/connection.php");

if(isset($_POST['update'])){ 

$id = $_GET['id'];

$header = $_POST['header'];
$content = $_POST['content'];


$sql="UPDATE mot SET header = '$header', content = '$content' WHERE id='$id'";
$result=mysql_query($sql);


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

else {
echo "ERROR";
}
}
?>

 

Any help would be much appreciated

Link to comment
Share on other sites

In your third piece of code, $_GET['id'] doesn't exist. Your update form is passing the id as a $_POST variable.

 

You need to have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will report and display all the errors is detects. You will save a TON of time. The undefined $_GET['id'] variable would have been producing an error message that would have helped you find this simple problem.

Link to comment
Share on other sites

Thanks for your quick reply, it half working... it's updating the database but replacing the fields with no content

 

there are 2 errors left in the code which am confused about. error below:

Notice: Undefined index: header in E:\Domains\o\onlinegurusites.co.uk\user\htdocs\garageRus\admin\update_ac.php on line 9

Notice: Undefined index: content in E:\Domains\o\onlinegurusites.co.uk\user\htdocs\garageRus\admin\update_ac.php on line 10
Successful
View result

 

these are the problem lines of code;

 

$header = $_POST['header'];
$content = $_POST['content'];

 

Thanks and thanks in advance for your help!!

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.