Joined: Tue Aug 09, 2011 12:50 pm Posts: 7 Has thanked: 0 time Have thanks: 0 time
hi all, i have written a code in php for inserting and deleting multiple records using check boxes."test" is the database name. "emp" is the table name which contains the following records in my database.
empno empname desig 1111 raju pilot 1112 ram chef 1113 ramu doctor 1114 paul engineer 1115 ajay player
now i want to delete one or more records and insert the records in to the above table.. i have written the below code for deleting but the table values are not displaying and i need to delete also using check box. below is delete_multiple2.php
Code:
<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="emp"; // Table name // Connect to server and select databse. mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); // Check if delete button active, start this if(isset($_POST['delete'])){ if(sizeof($_POST['chkbx'])){ foreach($_POST['chkbx'] AS $val){ $sql = mysql_query("DELETE FROM $tbl_name WHERE id=".(int)$val)or die(mysql_error()); } } // if successful redirect to delete_multiple.php
if($sql){ header('Location:delete_multiple2.php');//THIS WAS SAMPLE.PHP - YOU MIGHT WANT TO CHANGE IT BACK } } $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?>
also i have written for inserting records into database. but it is not inserting..... below is insert.php
Code:
<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="emp"; // Table name
// Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql);
// Get values from form $no=$_POST['empno']; $name=$_POST['empname']; $desig=$_POST['desig'];
// Check if button name "Submit" is active, do this if($checkbox){ for($i=0;$i<$count;$i++) { $sql1="INSERT INTO $tbl_name (empno, empname, desig)VALUES('$no[$i]', '$name[$i]', '$desig[$i]')"; $result1=mysql_query($sql1); } }
i want to integrate these two things so that it will be easy for inserting and deleting any no of times.. kindly tell me how to do it.... where is my code went wrong.......
msi_333
Question subject: Re: inserting and deleting in php
Posted: Sat Aug 20, 2011 4:44 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
Your code looks working well ,please post me the errors you are getting , also after you delete records you have this lines of code :
Code:
if($sql){ header('Location:delete_multiple2.php');//THIS WAS SAMPLE.PHP - YOU MIGHT WANT TO CHANGE IT BACK }
it moves you to different page.
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )