Data not being inserted into a mysql db
| From: | Stuart McDonald | Date: | Sat, 09 Sep 2000 00:41:58 +0000 |
| Subject: | Data not being inserted into a mysql db | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-2759@lists.php.net to get a copy of this message | ||
I've got a simple form for entering data into a mysql db. Once the data is
entered, the user is given a confirmation field and once confirmed it is
written to the db.
Yet, sometimes it does not actually write to the db. This happens on a
seemingly random basis, and is very tedious. When I insert the information
on my local machine, I have no problems. Could this be a connection related
problem?
Here is the form:
____start_____
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Sambrown add or update story records</title>
</head>
<body>
<h3>Add or Update Story Records</h3>
<?php
require ("../includes/common.php3");
if($verify){
echo "<form method=\"post\" action=\"$PHP_SELF\">\n";
echo "<input type=\"hidden\" name=\"storyname\"
value=\"$storyname\">\n";
echo "<input type=\"hidden\" name=\"storydate\"
value=\"$storydate\">\n";
echo "<input type=\"hidden\" name=\"pid\"
value=\"$pid\">\n";
echo "<input type=\"hidden\" name=\"intro\"
value=\"$intro\">\n";
echo "<input type=\"hidden\" name=\"story\"
value=\"$story\">\n";
echo "<input type=\"hidden\" name=\"pictureaddress\"
value=\"$pictureaddress\">\n";
echo "<input type=\"hidden\" name=\"keywords\"
value=\"$keywords\">\n";
echo "<input type=\"hidden\" name=\"cid\"
value=\"$cid\">\n";
echo "<P>Is the following information correct?</P>\n";
echo "<p>Storyname: $storyname</p>\n";
echo "<p>Story Date: $storydate</p>\n";
echo "<p>Pid: $pid</p>\n";
echo "<p>Introduction: $intro</p>\n";
echo "<p>Story: $story</p>\n";
echo "<p>Picture Address: $pictureaddress</p>\n";
echo "<p>Keywords: $keywords</p>\n";
echo "<p>Cid: $cid</p>\n";
echo "<input type=\"Submit\" name=\"submit\" value=\"Enter or
update
record\">\n";
exit();
}
if($submit) {
// here if no storyID then adding else editing
if($sid) {
$sql = "UPDATE sam_story SET
storyname='$storyname',storydate='$storydate',pid='$pid',intro='$intro',stor
y='$story',pictureaddress='$pictureaddress',keywords='$keywords',cid='$cid'
WHERE sid=$sid";
} else {
$sql = "INSERT INTO sam_story
(storyname,storydate,pid,intro,story,pictureaddress,keywords,cid) VALUES
('$storyname','$storydate','$pid','$intro','$story','$pictureaddress','$keyw
ords','$cid')";
}//end of inner if
file://run the SQL to the database
$result = mysql_query($sql);
echo "<P>The information has been updated/edited.<br><a
href=\"modify_sam.php3\">Click here to return to modify
records</a></P>\n";
}elseif ($delete){
file://to delete a record
$sql = "DELETE FROM sam_story WHERE sid=$sid";
$result=mysql_query($sql);
echo "<P>$sql record deleted!</P>\n";
} else {
file://this happens if we don't press submit
if (!$sid) {
file://print the list if there is no editing
$result=mysql_query("SELECT * FROM sam_story",$db);
?>
<table bgcolor="#cccc33" summary= "table to hold form details"
width="590"
cellpadding="1" cellspacing="1" border="1">
<tr><td><b>Story ID</b></td><td><b>Story
Name</b></td><td><b>Options</b></td></tr>
<?php
while ($myrow = mysql_fetch_array($result)) {
printf("\t<tr><td>%s</td><td><a
href=\"sambrown_modify_story.php3%s?sid=%s\">%s</a></td><td><a
href=\"sambrown_modify_story.php3%s?sid=%s&delete=yes\">(DELETE)</a> </
td></tr>\n", $myrow["sid"],$PATH_SELF, $myrow["sid"],
$myrow["storyname"],$PATH_SELF, $myrow["sid"], $PATH_SELF,
$myrow["sid"]);
}
?>
</table>
<?php
}//end of if
?>
<P><a href="<?php echo $PHP_SELF?>">Add a record</a></P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($sid) {
file://editing so select a record
$sql = "SELECT * FROM sam_story WHERE sid=$sid";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$sid = $myrow["sid"];
$storyname = $myrow["storyname"];
$storydate = $myrow["storydate"];
$intro = $myrow["intro"];
$story = $myrow["story"];
$pictureaddress = $myrow["pictureaddress"];
$keywords=$myrow["keywords"];
$pid=$myrow["pid"];
$cid=$myrow["cid"];
file://print the id for editing
?>
<input type=hidden name="sid" value="<?php echo $myrow["sid"]
?>">
<?php
}//end of if
?>
<table bgcolor="#cccc33" summary= "table to hold form details"
width="590"
cellpadding="1" cellspacing="1" border="1">
<tr>
<td><P><b>Story Name:</b></p></td><td><p><input
type="Text"
name="storyname" size="75" value="<?php echo $storyname
?>"></P></td>
</tr>
<tr>
<td><P><b>Story Date:</b></p></td><td><p><input
type="Text"
name="storydate" size="75" maxlength="255" value="<?php echo
$storydate
?>"><br>Enter the date in the format:yyyy-mm-dd</P></td>
</tr>
<tr>
<td><P><b>Intro:</b><br>No HTML formatting is needed inthis
section.</p></td><td><p><textarea cols="65" rows="25"
name="intro"
wrap="hard"><?php echo $intro ?></textarea></P></td>
</tr>
<tr>
<td><P><b>Story:</b><br>All paragraphs need a <p> and
</p> and
HTML tags for italica and bold can also be
entered.<p></p></td><td><p><textarea cols="65"
rows="25" name="story"
wrap="hard"><?php echo $story ?></textarea></P></td>
</tr>
<tr>
<td><P><b>Picture
address:</b></p></td><td><p><input type="Text"
name="pictureaddress" size="75" maxlength="255" value="<?php
echo
$pictureaddress ?>"></P></td>
</tr>
<tr>
<td><P><b>Keywords:</b><br>Separate these by a comma, up to 255
characters
allowed.</p></td><td><p><textarea cols="65" rows="5"
name="keywords"
wrap="hard"><?php echo $keywords ?></textarea></P></td>
</tr>
<tr>
<td><p><b>Published in</b></p>
</td>
<td>
<select name="pid" size="5">
<?php
if($sid){
file://get selected values
$result_pid = mysql_query("SELECT p.pid,p.publisher FROM sam_publisher p
WHERE pid=$pid",$db);
$myrow = mysql_fetch_array($result_pid);
do {
printf("\t<option value=\"%s\"
selected>%s\n",$myrow["pid"],$myrow["publisher"]);
} while ($myrow = mysql_fetch_array($result_pid));
}else{
file://get selected values
$result_pid_empty = mysql_query("SELECT * FROM sam_publisher",$db);
$myrow = mysql_fetch_array($result_pid_empty);
do {
printf("\t<option
value=\"%s\">%s\n",$myrow["pid"],$myrow["publisher"]);
} while ($myrow = mysql_fetch_array($result_pid_empty));
}
?>
</select>
</td>
</tr>
<tr>
<td><p><b>Category</b></p></td>
<td> <select name="cid" size="5">
<?php
if($sid){
file://get selected values
$result_cid = mysql_query("SELECT c.cid,c.category FROM sam_category c
WHERE cid=$cid",$db);
$myrow = mysql_fetch_array($result_cid);
do {
printf("\t<option value=\"%s\"
selected>%s\n",$myrow["cid"],$myrow["category"]);
} while ($myrow = mysql_fetch_array($result_cid));
}else{
file://get selected values
$result_cid_empty = mysql_query("SELECT * FROM sam_category",$db);
$myrow = mysql_fetch_array($result_cid_empty);
do {
printf("\t<option
value=\"%s\">%s\n",$myrow["cid"],$myrow["category"]);
} while ($myrow = mysql_fetch_array($result_cid_empty));
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="Submit" name="verify"
value="Enter or update
record"></td>
</tr>
</table>
</form>
<?php
}//end of whole if
?>
</body>
</html>
____ends____
Any suggestions would be much appreciated.
Stuart