Forum Moderators: coopster

Message Too Old, No Replies

Code Error - Display, Insert and Edit

Can anyone help with this code?

         

m4tt

7:28 am on Jul 15, 2005 (gmt 0)

10+ Year Member



Can anyone help with this code, it displays perfectly, it deletes ok, however it won't edit or insert data? It comes up with record updated/edited ok however nothing has been added and when editing I select a record and nothing comes up in the form..

<?php

include("db.inc.php");
$db = mysql_connect(localhost, $username, $password);

mysql_select_db($database,$db);

if ($submit) {

// here if no ID then adding else we're editing

if ($id) {

$sql = "UPDATE sa SET name='$name',address='$address_line1',suburb='$address_line2',city='$city',
state='$state',postcode='$postcode',phone='$phone',fax='$fax',email='$email',web='$web' WHERE id='{$_GET['id']}'";

} else {


$name = $_POST["name"];

$address_line1 = $_POST["address"];

$address_line2 = $_POST["suburb"];

$city = $_POST["city"];

$state = $_POST["state"];

$postcode = $_POST["postcode"];

$phone = $_POST["phone"];

$fax = $_POST["fax"];

$email = $_POST["email"];

$web = $_POST["web"];

$sql = "INSERT INTO sa (name,address,suburb,city,postcode,state,postcode,phone,fax,email,web) VALUES ('$name','$address_line1','$address_line2','$city','$state','$postcode','$phone','$fax','$email','$web')";

}

// run SQL against the DB

$result = mysql_query($sql);

echo "Record updated/edited!<p>";

} elseif ($delete) {

// delete a record

$sql = "DELETE FROM sa WHERE id='{$_GET['id']}'";

$result = mysql_query($sql);

echo "$id Record deleted!<p>";

} else {

// this part happens if we don't press submit

if (!$id) {

// print the list if there is not editing

$result = mysql_query("SELECT * FROM sa",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["name"], $myrow["suburb"]);

printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);

}

}

?>

<P>

<a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>

<P>

<form method="post" action="<?php echo $PHP_SELF?>">

<?php

if ($id) {

// editing so select a record

$sql = "SELECT * FROM sa WHERE id={$_GET['id']}";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

$id = $myrow["id"];

$name = $myrow["name"];

$address_line1 = $myrow["address"];

$address_line2 = $myrow["suburb"];

$city = $myrow["city"];

$state = $myrow["state"];

$postcode = $myrow["postcode"];

$phone = $myrow["phone"];

$fax = $myrow["fax"];

$email = $myrow["email"];

$web = $myrow["web"];


// print the id for editing

?>

<input type=hidden name="id" value="<?php echo $id?>">

<?php

}

?>

Name:<input type="Text" name="first" value="<?php echo $name?>"><br>

Address:<input type="Text" name="last" value="<?php echo $address_line1?>"><br>

Suburb:<input type="Text" name="address" value="<?php echo $address_line2?>"><br>

City:<input type="Text" name="suburb" value="<?php echo $city?>"><br>
State:<input type="Text" name="state" value="<?php echo $state?>"><br>
Postcode:<input type="Text" name="postcode" value="<?php echo $postcode?>"><br>
Phone:<input type="Text" name="phone" value="<?php echo $phone?>"><br>
Fax:<input type="Text" name="fax" value="<?php echo $fax?>"><br>
Email:<input type="Text" name="email" value="<?php echo $email?>"><br>
Web:<input type="Text" name="web" value="<?php echo $web?>"><br>

<input type="Submit" name="submit" value="Enter information">

</form>

<?php

}

?>

[edited by: coopster at 11:28 am (utc) on July 15, 2005]
[edit reason] fixed sidescroll [/edit]

coopster

11:26 am on Jul 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For certain your INSERT query syntax is incorrect. The number of columns in the column list does not match the number of columns in the VALUES list.

I'm guessing the UPDATE statement is more of the same. To check your statements you can dump the query to the browser

exit($sql);

Also, turn on error_reporting [php.net] during testing and use the mysql_error [php.net] functions to help you troubleshoot [webmasterworld.com].