Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php mysql update script problem

Status
Not open for further replies.

postyman

Vendor
Jan 27, 2005
6
GB
Hi

I am a newbie and am trying to adapt a script to update a database I have created, the script seems to be working fine in all aspects other than it does not actually update the mysql database. There are 3 parts to the script and I have enclosed then below. For security reasons I have XXX'd out the database info however I can confirm that each page is linking to my database OK.

Thanks for any advice given.

Terry

amend.php
-------------
<html><TD WIDTH="29%" HEIGHT="60"><DIV ALIGN="LEFT"><BR>Input
the Reference, to make sure we have the right one:<BR><BR><FONT SIZE="1">(quick
reference listed below for your convenience)</FONT></DIV>

<form method=POST action="amend1.php">
<DIV ALIGN="LEFT"><INPUT TYPE="text" NAME="record" SIZE="50" MAXLENGTH="50"><BR><BR>
<IMG SRC="search.jpg" WIDTH="51" HEIGHT="46" ALIGN="ABSMIDDLE" BORDER="1">
<INPUT TYPE="submit" NAME="go" VALUE="Search on that Input"></DIV></form></TD></TR>
<TR><TD WIDTH="29%"><DIV ALIGN="LEFT">

<?php
// Show simple format of the records so person can choose the reference name/number
// this is then passed to the next page, for all details
$db = mysql_connect("localhost", "XXXXXX", "XXXXXXXXX");
mysql_select_db("XXXXXXXXX",$db) or die ('Unable to connect to database');

$q="SELECT * FROM members ORDER BY last ASC";

$result = mysql_query( $q, $db )
or die(" - Failed More Information:<br><pre>$q</pre><br>Error: " . mysql_error());

$num_rows = mysql_num_rows($result);
if ($myrow = mysql_fetch_array($result)) {

echo "<br>A Quick View<BR><br>";
echo "<table border=1>\n";
echo "<tr><td><b>ID No:</b></td><td>First Name:</td><td>Last Name:</td><td>Address:</td></tr>\n";
do {
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>
</tr>\n", $myrow["id"], $myrow["first"], $myrow["last"], $myrow["address"]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
} else {
echo "$ref: That record appears to be unavailable";
}

mysql_free_result($result);
mysql_close($db);
?></DIV></TD></html>


amend1.php
----------

<?PHP
session_start();
?>
<HTML>

<?php
$id = $_POST['record'];
echo "id: $record<br><BR>";

$host = "localhost";
$login_name = "XXXXXXXXX";
$password = "XXXXXXXXX";

//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");

//Select the database we want to use
mysql_select_db("XXXXXXXXXX") or die("Could not find database");

$result=mysql_query(" SELECT * FROM members WHERE id='$record'");
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {


// collect all details for our one reference
$id=mysql_result($result,$i,"id");
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$address=mysql_result($result,$i,"address");
$postcode=mysql_result($result,$i,"postcode");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$email=mysql_result($result,$i,"email");
$kin=mysql_result($result,$i,"kin");
$memtype=mysql_result($result,$i,"memtype");
$paymethod=mysql_result($result,$i,"paymethod");
$sdate=mysql_result($result,$i,"sdate");
$edate=mysql_result($result,$i,"edate");
$nolinesat=mysql_result($result,$i,"nolinesat");
$nolinewed=mysql_result($result,$i,"nolinewed");
$nolineboth=mysql_result($result,$i,"nolineboth");
$noforsat=mysql_result($result,$i,"noforsat");
$noforwed=mysql_result($result,$i,"noforwed");
$noforboth=mysql_result($result,$i,"noforboth");
$nick=mysql_result($result,$i,"nick");


//next we display only the details we want to allow to be changed in a form object
// the other details that we won't allow to be changed can be echoed to the screen
//note the hidden input line 3 below. We don't need to echo it to the screen
?>

<TABLE WIDTH="100%" CELLPADDING="10" CELLSPACING="0" BORDER="2"> <TR ALIGN="center" VALIGN="top">
<TD ALIGN="center" COLSPAN="1" ROWSPAN="1" BGCOLOR="#F2F2F2">

<FORM ACTION="amend2.php" METHOD="post">
<P ALIGN="LEFT">
<INPUT TYPE="hidden" NAME="ud_id" VALUE="<? echo "$id" ?>">

<BR>First Name:<BR><INPUT TYPE="text" NAME="ud_first" VALUE="<? echo "$first"?>"></P>

<P ALIGN="LEFT">Last Name:<BR><INPUT TYPE="text" NAME="ud_last" VALUE="<? echo "$last"?>"></P><HR><B>
</B>

<P ALIGN="LEFT">Address:<BR> <INPUT TYPE="text" NAME="ud_address" VALUE="<? echo "$address"?>" SIZE="30" MAXLENGTH="50"></P>

<P ALIGN="LEFT">Post Code:<BR> <INPUT TYPE="text" NAME="ud_postcode" VALUE="<? echo "$postcode"?>" SIZE="30" MAXLENGTH="10"></P>
</P>

<P><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"> </P></FORM></TD></TR></TABLE>

<?
++$i;
}
?>


amend2.php
---------------
<?PHP
session_start();
?>

<?php

$ud_id=$_POST['ud_id'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_address=$_POST['ud_address'];
$ud_postcode=$_POST['ud_postcode'];


if ($ud_id == "") echo "! No identifier retrieved";
else
echo "Amending record $ud_id";

//clean up any carriage returns etc
$ud_id = preg_replace("/[\n\r]*/","",$ud_id);
$ud_first = preg_replace("/[\n\r]*/","",$ud_first);
$ud_last = preg_replace("/[\n\r]*/","",$ud_last);
$ud_address = preg_replace("/[\n\r]*/","",$ud_address);
$ud_postcode = preg_replace("/[\n\r]*/","",$ud_postcode);

$host = "localhost";
$login_name = "XXXXXXXXXX";
$password = "XXXXXXXXX";

//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");

//Select the database we want to use
mysql_select_db("XXXXXXXXX") or die("Could not select database");

mysql_query(" UPDATE members SET first='$ud_first', last='$ud_last', address='$ud_address',
postcode='$ud_postcode', WHERE reference ='$ud_id'");

echo "<BR>Record $ud_id <-- Updated<BR><BR>";
?>

<?php
//if you want to check it's ok, display new data

echo "Search on $ud_id<BR>";

$db = mysql_connect("localhost", "XXXXXXX", "XXXXXXX");
mysql_select_db("XXXXXXXX",$db) or die ('Unable to connect to database');

$q="SELECT * FROM members WHERE id ='$ud_id'";

$result = mysql_query( $q, $db )
or die(" - Failed More Information:<br><pre>$q</pre><br>Error: " . mysql_error());

$num_rows = mysql_num_rows($result);
if ($myrow = mysql_fetch_array($result)) {



echo "<table border=0>\n";
echo "<tr><td></td><td></td><td></td><td></td></tr>\n";


do {
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>
</tr>\n", $myrow["first"], $myrow["last"], $myrow["address"], $myrow["postcode"]);

} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
} else {
echo "Sorry, no records were found";
}

mysql_free_result($result);
mysql_close($db);
session_destroy();

?>

 
Just glancing quickly, there seems to be a spurious comma in this line:

mysql_query(" UPDATE members SET first='$ud_first', last='$ud_last', address='$ud_address',
postcode='$ud_postcode'[red],[/red] WHERE reference ='$ud_id'");

The comma in red shouldn't be there.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hi sleipnir214

Thats it, soted now works fine. I have spent 3 days on this.

Thank you very much indeed. GREAT JOB.

Terry
 
No problem. I just happened to catch that one easily.

I have published a FAQ in this forum titled "Debugging PHP code" (faq434-2999), which has some helpful general advice on database connectivity debugging (Section 1.5).


To reiterate some of the things in the FAQ, I recommend you add more error-detection and reporting to your code. If line had read:

[blue]$foo = [/blue]mysql_query(" UPDATE members SET first='$ud_first', last='$ud_last', address='$ud_address',
postcode='$ud_postcode', WHERE reference ='$ud_id'");

[blue]if ($foo == FALSE)
{
die ('Could not insert' . mysql_error());
}[/blue]

then your script code would have given you some indication what was wrong.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top