Jump to content


Photo

MySQL/PHP script - where am i going wrong?


  • Please log in to reply
2 replies to this topic

#1 oj_boy

oj_boy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 09 November 2003 - 05:09 PM

Apologies for posting entire script but just can\'t seem to figure out the problem - it won\'t save data once it\'s been updated. It had been working until I added another column. Can anyone see anything obvious?

<?php$db = mysql_connect("localhost", "XXXX", "XXXX");mysql_select_db("wobble",$db);if ($submit) {  // here if no ID then adding else we\'re editing  if ($id) {    $sql = "UPDATE contacts SET org=\'$org\',contact=\'$contact\',address=\'$address\',tel=\'$tel\',mob=\'$mob\',fax=\'$fax\',email=\'$email\',web=\'$web\',info=\'$info,type=\'$type\' WHERE id=$id";  } else {    $sql = "INSERT INTO contacts (org,contact,address,tel,mob,fax,email,web,info,type) VALUES (\'$org\',\'$contact\',\'$address\',\'$tel\',\'$mob\',\'$fax\',\'$email\',\'$web\',\'$info\',\'$type\')";  }  // run SQL against the DB  $result = mysql_query($sql);  echo "Record updated/edited!<p>";  echo "<a href="database.php">SHOW WHOLE DATABASE / ADD A NEW RECORD</a><p>";  } elseif ($delete) {	// delete a record    $sql = "DELETE FROM contacts WHERE id=$id";	    $result = mysql_query($sql);    echo "$sql Record deleted!<p>";	echo "<a href="database.php">SHOW WHOLE DATABASE / ADD A NEW RECORD</a><p>";} else {  // this part happens if we don\'t press submitprint ("<form method "POST" action="$PHP_SELF">Search database: <input name="name" size="30" maxlength="255"> ");print ("<input type="Submit" name="sub" value="Search"></form>");if ($sub) {// print the search results    $result = mysql_query("SELECT * FROM contacts WHERE org LIKE \'%$name%\' OR contact LIKE \'%$name%\' OR type LIKE \'%$name%\' ORDER BY type, org, contact",$db);	echo "<table>n";	echo "<tr><td><div align="center"><strong>Contact Name</strong></div></td><td><div align="center"><strong>Organisation Name</strong></div></td><td><div align="center"><strong>Telephone</strong></div></td><td><div align=center"><strong>Email</strong></div></td><td><div align="center"><strong>Type</strong></div></td></tr>n";  while ($myrow = mysql_fetch_row($result)) {  	  	printf("<tr><td><a href="%s?id=%s">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>n", $PHP_SELF, $myrow[0], $myrow[2], $myrow[1], $myrow[4], $myrow[7], $myrow[10]);  }  	echo "</table>n";			}  elseif (!$id) {    // print the list if there is not editing    $result = mysql_query("SELECT * FROM contacts WHERE org LIKE \'%$name%\' OR contact LIKE \'%$name%\' OR type LIKE \'%$name%\' ORDER BY type",$db) or die ("Sorry no results. <a href="javascript:history.go(-1);">Please try again</a>.");	echo "<table>n";	echo "<tr><td><div align="center"><strong>Contact Name</strong></div></td><td><div align="center"><strong>Organisation Name</strong></div></td><td><div align="center"><strong>Telephone</strong></div></td><td><div align=center"><strong>Email</strong></div></td><td><div align="center"><strong>Type</strong></div></td></tr>n";  while ($myrow = mysql_fetch_row($result)) {  	  	printf("<tr><td><a href="%s?id=%s">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>n", $PHP_SELF, $myrow[0], $myrow[2], $myrow[1], $myrow[4], $myrow[7], $myrow[10]);	}	echo "</table>n";	  }  ?> <P> <a href="<?php echo $PHP_SELF?>">SHOW WHOLE DATABASE / ADD A NEW RECORD</a> <P>  <form method="post" action="<?php echo $PHP_SELF?>">  <?php  if ($id) {    // editing so select a record    $sql = "SELECT * FROM contacts WHERE id=$id";    $result = mysql_query($sql);    $myrow = mysql_fetch_array($result);    $id = $myrow["id"];    $org = $myrow["org"];    $contact = $myrow["contact"];    $address = $myrow["address"];    $tel = $myrow["tel"];		$mob = $myrow["mob"];		$fax = $myrow["fax"];		$email = $myrow["email"];		$web = $myrow["web"];		$info = $myrow["info"];		$type = $myrow["type"];    // print the id for editing    ?>  <p>     <input type=hidden name="id" value="<?php echo $id ?>">    <?php  }  ?>    Contact Name:     <input type="Text" name="contact" value="<?php echo $contact ?>">    <br>    Organisation Name:     <input type="Text" name="org" value="<?php echo $org ?>">    <br>    Address:     <textarea name="address" cols="30" rows="4"><?php echo $address ?></textarea>    <br>    Telephone:     <input type="Text" name="tel" value="<?php echo $tel ?>">    <br>    Mobile:     <input type="Text" name="mob" value="<?php echo $mob ?>">    <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>    Information:     <textarea name="info" cols="30" rows="4"><?php echo $info ?></textarea>    <br>    Type:     <select name="type">      <option value="Funding">Funding</option>      <option value="Press">Press</option>      <option value="Musician">Musician</option>      <option value="Stall">Stall</option>      <option value="Act">Act</option>      <option value="Other">Other</option>      <option value="<?php echo $type ?>" selected><?php echo $type ?></option>    </select>    <br>    <input type="Submit" name="submit" value="Enter information">     <input type="Submit" name="delete" value="Delete record">  </p>  </form><?php}?>

Sorry once again

tom.

#2 Glish

Glish
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 November 2003 - 06:17 AM

i didnt read all the code, I said to myself I\'ll stop at the first error. Submit the error message please, would be easier

here is one error (in your first update)
$sql = \"UPDATE contacts SET org=\'$org\',contact=\'$contact\',address=\'$address\',tel=\'$tel\',mob=\'$mob\',fax=\'$fax\',email=\'$email\',web=\'$web\',info=\'$info,type=\'$type\' WHERE id=$id\";

you are missing a \'

Like I said i dont know if the rest is ok

#3 oj_boy

oj_boy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 November 2003 - 07:40 PM

thanks!

yep that missing apostrophe was the problem. huge apologies for posting the whole script.

i have no idea how i managed to miss it, i\'d been through the code so many times.

thanks once more

tom.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users