Adnan628 Posted May 12, 2011 Share Posted May 12, 2011 Hello there, I'm quite new on PHP language.I'm trying to make an simple Phone book apps.I added insert & show options in Phone book.Now i want to add "update & delete" option.How can i do that. This is add code: <?php mysql_connect("localhost","admin",""); mysql_select_db("simphp"); //Get Values from form $name=$_POST['name']; $email=$_POST['email']; $number=$_POST['number']; mysql_query("insert into phonebook(name,email,number) values('$name','$email','$number')"); echo"Saved Sucessfully"; echo "<br/>"; echo '<a href="index.php/">Homepage</a>'; echo "<br/>"; echo '<a href="select.php/">Show Data</a>'; echo "<br/>"; echo '<a href=update.php>Update</a>'; mysql_close(); ?> Show.php=>> <?php mysql_connect("localhost","admin",""); mysql_select_db("simphp"); $query="select *from phonebook"; $result=mysql_query($query); $num=mysql_numrows($result); echo"Number of records Stored:$num"; $i=0; while($i<$num) { $name=mysql_result($result,$i,"Name"); $email=mysql_result($result,$i,"Email"); $number=mysql_result($result,$i,"number"); echo "<br/>"; echo "Name :<b> $name </b><br/>"; echo "<br/>"; echo "Email : <b>$email</b> <br/>"; echo "<br/>"; echo "Phone :<b>$number</b><br/>"; echo '<a href=update.php>Update</a>'; $i++; } ?> How can i update and delete data?? :confused:help me Quote Link to comment Share on other sites More sharing options...
btherl Posted May 12, 2011 Share Posted May 12, 2011 Does your phone book table have a primary key? Quote Link to comment Share on other sites More sharing options...
fugix Posted May 12, 2011 Share Posted May 12, 2011 you will want to look here and here Quote Link to comment Share on other sites More sharing options...
Adnan628 Posted May 12, 2011 Author Share Posted May 12, 2011 Does your phone book table have a primary key? Yes,Id is the primary key Quote Link to comment Share on other sites More sharing options...
btherl Posted May 12, 2011 Share Posted May 12, 2011 Ok, those links fugix gave are the manuals for the SQL commands you need. In each case you will want "WHERE id = $id" to ensure that you update or delete only the requested data, and not any other. Apart from that, updating and deleting is not much different from inserting new data. The id can be passed with a hidden input in the form. Quote Link to comment Share on other sites More sharing options...
samato Posted May 12, 2011 Share Posted May 12, 2011 If you are using phpMyAdmin as a database manager, you do realize you can generate MySQL which you can then implement in your code, right? An update query looks like this: $u = "UPDATE `table_name` SET (`column1`='$value1',`column2`='$value2') WHERE `primary_key`='$primary_key'"; mysql_query($u); Naturally you have to replace the generic names with the correct names and the variables with the variables specified in your code above. Delete looks like this: $d = "DELETE FROM `table_name` WHERE `primary_key`='$primary_key'"; mysql_query($d); Again, replace the generic names and variables as needed. Quote Link to comment Share on other sites More sharing options...
Adnan628 Posted May 12, 2011 Author Share Posted May 12, 2011 Ok, those links fugix gave are the manuals for the SQL commands you need. In each case you will want "WHERE id = $id" to ensure that you update or delete only the requested data, and not any other. Apart from that, updating and deleting is not much different from inserting new data. The id can be passed with a hidden input in the form. Update.php=>> <?php $id=$_GET['id']; mysql_connect("localhost","admin",""); mysql_select_db("simphp"); $query="SELECT * FROM contents WHERE id='$id'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $name=mysql_result($result,$i,"Name"); $email=mysql_result($result,$i,"Email"); $number=mysql_result($result,$i,"number"); ?> <html><head></head><body> <form action="updated.php" method="post"> <input type="hidden" name="ud_id" value="<? echo $id; ?>"> Name: <input type="text" name="ud_first" value="<? echo $first; ?>"><br> Email: <input type="text" name="ud_email" value="<? echo $email; ?>"><br> Number: <input type="text" name="ud_number" value="<? echo $number; ?>"><br> </form></body></html> <?php $i++;}?> i got this:-> Notice: Undefined index: id in D:\xampp\htdocs\simphp\update.php on line 2 Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\simphp\update.php on line 7 Quote Link to comment Share on other sites More sharing options...
btherl Posted May 12, 2011 Share Posted May 12, 2011 If the input is named "ud_id", you need to access $_GET['ud_id'] in your code. That is the cause of the first warning. The second warning is an uncaught error resulting from the query failing. I recommend replacing your query with this: $result=mysql_query($query) or die("Error in $query: " . mysql_error()); That will give you more information about why the query failed. The likely reason is that the id wasn't fetched from the form. Quote Link to comment Share on other sites More sharing options...
fugix Posted May 12, 2011 Share Posted May 12, 2011 looks like you forgot an underscore in your numrows line this.... $num=mysql_numrows($result); should be $num=mysql_num_rows($result); Quote Link to comment Share on other sites More sharing options...
fugix Posted May 12, 2011 Share Posted May 12, 2011 also, in your sql query...since your id is a number and not a string, it does not require you to encase it in single quotes...this will work $query="SELECT * FROM contents WHERE id=$id"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.