Jump to content

update / edit funtion


Birdmansplace

Recommended Posts

I am working on code to be able to recall and entry and edit it and submit it the changes.  first set of code reads the db,  second set is the info from db to be changed and third is the update code itself.  I am having errors with the third set of code. heres everything.  errors are listed above the third set of code.  i have spent a few hours on this and just cant figure out why its doing what its doing.  I found this code here http://www.phpeasystep.com/mysql/9.html and editing it to my needs.

 

thanks for takin the time and the help

 

first

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
$host="localhost"; // Host name
$username="#"; // Mysql username
$password="#"; // Mysql password
$db_name="#"; // Database name
$tbl_name="#"; // Table name

// 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");

$sql="SELECT * FROM $tbl_name";
$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>stitle</strong></td>
<td align="center"><strong>datepicker</strong></td>
<td align="center"><strong>sbody</strong></td>
<td align="center"><strong>ipaddress</strong></td>
<td align="center"><strong>date</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['stitle']; ?></td>
<td><? echo $rows['datepicker']; ?></td>
<td><? echo $rows['sbody']; ?></td>
<td><? echo $rows['ipaddress']; ?></td>
<td><? echo $rows['date']; ?></td>

<td align="center"><a href="update.php?sid=<? echo $rows['sid']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

 

second

<?
ini_set("display_errors", "10");
error_reporting(E_ALL);

include("dbinfo.php");

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


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


// Retrieve data from database
$sql="SELECT * FROM simple_search WHERE sid='$sid'";
$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"><strong>stitle</strong></td>
<td align="center"><strong>datepicker</strong></td>
<td align="center"><strong>sbody</strong></td>
<td align="center"><strong>ipaddress</strong></td>
<td align="center"><strong>date</strong></td>
</tr>
<tr>

<td><input name="stitle"     type="text" id="stitle"     value="<? echo $rows['stitle']; ?>" size="15"></td>
<td><input name="datepicker" type="text" id="datepicker" value="<? echo $rows['datepicker']; ?>"></td>
<td><input name="sbody"      type="text" id="sbody"      value="<? echo $rows['sbody']; ?>" size="15"></td>
<td><input name="ipaddress"  type="text" id="ipaddress"  value="<? echo $rows['ipaddress']; ?>" size="15"></td>
<td><input name="date"       type="text" id="date"       value="<? echo $rows['date']; ?>" size="15"></td>

</tr>
<tr>
<td> </td> 
<td><input name="id" type="hidden" id="id" value="<? echo $rows['sid']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
// close connection
mysql_close();
?>

 

third

 

errors i get with third set of code:

Notice: Undefined variable: stitle in /test/update_ac.php on line 17

Notice: Undefined variable: datepicker in /test/update_ac.php on line 17

Notice: Undefined variable: sbody in /test/update_ac.php on line 17

Notice: Undefined variable: sid in /test/update_ac.php on line 17

Successful

View result

 

<?php
ini_set("display_errors", "10");
error_reporting(E_ALL);

$host="localhost"; // Host name
$username="#"; // Mysql username
$password="#"; // Mysql password
$db_name="#"; // Database name
$tbl_name="#"; // Table name

// 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");


// update data in mysql database
$sql="UPDATE $tbl_name SET stitle='$stitle', datepicker='$datepicker', sbody='$sbody' WHERE sid='$sid'";
$result=mysql_query($sql);

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

else {
echo "ERROR";
}

?>

Link to comment
Share on other sites

First of all fix these. You dont echo like this

<? echo $rows['stitle']; ?>

 

You do like this with the short tags (which i dont prefer to use at all)

<?=$rows['stitle']; ?>

 

or (best practise)

<?php echo $rows['stitle']; ?>

Link to comment
Share on other sites

The variables $stitle, $datepicker, $sbody and $sid are not defined in update_ac.php. Variables need to be defined before you can use them.

 

Line 17 in update_ac.php is where you using these undefined variables

$sql="UPDATE $tbl_name SET stitle='$stitle', datepicker='$datepicker', sbody='$sbody' WHERE sid='$sid'";

 

I suspect these variable are from your form. In which case you need to get them from the $_POST superglobal variable first. Example

$stitle = $_POST['stitle'];
$datepicker = $_POST['datepicker'];
// etc 

Link to comment
Share on other sites

there is no "entry form" for this.  i am using a db that is no longer used.  just using the db for testing.  i have tried defining them an all 3 pages and still get the same errors.  there is a form setup for where the info is to be edited and thats it.  I am half temped to try setting up the edit form as a new entry when you click submit.  just cant figure it out.  I want to learn this with help but you might just have to flat out code it for me so i can follow it and understand it.

 

thanks

Link to comment
Share on other sites

This is your form input fields.

<td><input name="stitle"     type="text" id="stitle"     value="<? echo $rows['stitle']; ?>" size="15"></td>
<td><input name="datepicker" type="text" id="datepicker" value="<? echo $rows['datepicker']; ?>"></td>
<td><input name="sbody"      type="text" id="sbody"      value="<? echo $rows['sbody']; ?>" size="15"></td>
<td><input name="ipaddress"  type="text" id="ipaddress"  value="<? echo $rows['ipaddress']; ?>" size="15"></td>
<td><input name="date"       type="text" id="date"       value="<? echo $rows['date']; ?>" size="15"></td>

Each fields value is stored within the $_POST superglobal array when the form is submitted.

 

The form is submitted to update_ac.php. In this file before line 17 you need to extract these values from the $_POST superglobal array. Here is an example of getting the first two values from your form

$stitle = $_POST['stitle']; // get the value from the form field named "stitle"
$datepicker = $_POST['datepicker']; // get the value from the form field named "datepicker"
// etc 

See if you can work out how to get the remaining values from your form.

Link to comment
Share on other sites

wow not sure why i didnt think to put it there.  lol.  to much thinking going on and no sleep.

 

$sql="UPDATE $tbl_name SET stitle='$stitle', datepicker='$datepicker', sbody='$sbody' WHERE sid='$sid'";

 

now i just need to figure out $sid.  removed it from the code and it changed every entry to the same thing.

 

code up date

// 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");
$stitle = $_POST['stitle']; // get the value from the form field named "stitle"
$datepicker = $_POST['datepicker']; // get the value from the form field named "datepicker"
$sbody= $_POST['sbody'];
$sid=$_GET['sid'];
// update data in mysql database
$sql="UPDATE $tbl_name SET stitle='$stitle', datepicker='$datepicker', sbody='$sbody' WHERE sid='$sid'";
$result=mysql_query($sql);

Link to comment
Share on other sites

i added that above line 17 but at the end if the string and then i get this and for some reason its not passing it on.  also all "id" have been changed to "sid" cause thats what its named in the db

 

Parse error: syntax error, unexpected T_VARIABLE in update_ac.php on line 19

Link to comment
Share on other sites

<?php
ini_set("display_errors", "10");
error_reporting(E_ALL);

$host="localhost"; // Host name
$username="#"; // Mysql username
$password="#"; // Mysql password
$db_name="#"; // Database name
$tbl_name="#"; // Table name

// 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");

$stitle = $_POST['stitle']; // get the value from the form field named "stitle"
$datepicker = $_POST['datepicker']; // get the value from the form field named "datepicker"
$sbody= $_POST['sbody'];
$sid = $_POST['sid'];

// update data in mysql database
$sql="UPDATE $tbl_name SET stitle='$stitle', datepicker='$datepicker', sbody='$sbody' WHERE sid='$sid'";
$result=mysql_query($sql);



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

else {
echo "ERROR";
}

?>

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.