Jump to content

MySQL/PHP script - where am i going wrong?


oj_boy

Recommended Posts

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 submit





print ("<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.

Link to comment
https://forums.phpfreaks.com/topic/1331-mysqlphp-script-where-am-i-going-wrong/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.