Jump to content

inserting and deleting using php

- - - - -

  • Please log in to reply
3 replies to this topic

#1
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
hi all,
i have written a script for deleting more than one records using check box.
but it is not working.below is the code ...

<script>

function fundel(sno)

{

rv=confirm("u want to delete");

if(rv==true)

{

location="delete.php?seno="+sno;

}

}

</script>

<form method="post" action="delrec.php">

<table border='1'>

<?php

mysql_connect("localhost","root","");

mysql_select_db("test");

$data=mysql_query("select * from emp");

while($rec=mysql_fetch_row($data))

{

echo"<tr><td><input type='checkbox' name=$rec[2]'

value='$rec[2]'><td>$rec[0]<td>$rec[1]<td>$rec[2]<td><input type='button' value='delete'

onclick='fundel($rec[2])'>";

}

?>

</table>

<input type='submit' value='delrec'>

</form>


below ids the delrec.php.

/*<?php

$qs=$_REQUEST['seno'];

mysql_connect("localhost","root","");

mysql_select_db("test");

mysql_query("delete from emp where sno=$qs");

header('location:getrec.php");

?>*/

<script>

function delrec(sno)

{

rv=confirm("u want to delete");

if(rv==true)

{

location="delete.php?seno="+sno;

}

}

</script>

also is the delete.php..

<?php

$qs=$_REQUEST['seno'];

mysql_connect("localhost","root","");

mysql_select_db("test");

mysql_query("delete from emp where sno=$qs");

header("location:getrec.php");

?>

/*<script>

function delrec(sno)

{

rv=confirm("u want to delete");

if(rv==true)

{

location="delete.php?seno="+sno;

}

}

</script>

*/



also i want to insert values of three fields using php code.
i have written the code.when i click the insert button it should get inserted
into database....
below is the code for insert function..

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">

<tr>

<td><form name="form1" method="post" action="ins.php">

<table width="100%" border="0" cellspacing="1" cellpadding="3">

<tr>

<td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>

</tr>

<tr>

<td width="71">Name</td>

<td width="6">:</td>

<td width="301"><input name="name" type="text" id="name"></td>

</tr>

<tr>

<td>Lastname</td>

<td>:</td>

<td><input name="lastname" type="text" id="lastname"></td>

</tr>

<tr>

<td>Email</td>

<td>:</td>

</tr>

<tr>

<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>

</tr>

</table>

</form>

</td>

</tr>

</table>

also the ins.php..

<?php


/*$host="localhost"; // Host name 

$username=""; // Mysql username 

$password=""; // Mysql password 

$db_name="test"; // Database name 

$tbl_name="test_mysql"; // Table name*/


// Connect to server and select database.

mysql_connect("localhost", "root", "")or die("cannot connect"); 

mysql_select_db("test")or die("cannot select DB");


// Get values from form 

$empno=$_POST['empno'];

$empname=$_POST['empname'];

$desig=$_POST['desig'];


// Insert data into mysql 

$sql="INSERT INTO emp(empno, empname, desig)VALUES('$empno', '$empname', '$desig')";

$result=mysql_query($sql);


// if successfully insert data into database, displays message "Successful". 

if($result){

echo "Successful";

echo "<BR>";

echo "<a href='insert.php'>Back to main page</a>";

}


else 

{

echo "ERROR";

}

// close connection 

mysql_close();

?>

i want all these to be integrated into one form so that i can easily insert,delete using php
kindly tell me how to do it....

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
You could put everything in the same php page

At the top of the page put validate if the user want to insert or delete or simply load like this:

if (isset($_POST['insert']))

{

//Insert code

}

else if (isset($_POST['delete']))

{

//Delete code

}


And after that you put your code to fetch the data and show them
In your html code, you have to make 2 form, one to insert and one to delete
And in your forms, you add an input hidden with the correct name

<input type="hidden" name="insert" value="true" />

For the insert form


<input type="hidden" name="delete" value="true" />

For the delete form

And this should do the tricks

#3
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
do u think that below program will work where is the fundel() function in that.
it is unable to delete..
i need to delete using checkbox.for more than 1 records...
i need to insert directly using php so that it automatically geta inserts in to database...
still i have to use delete.php where to use it...
whether we need to save as a single programname.php the entire code...
or else we need to make each code as.php
tell me how to do it.....


if (isset($_POST['insert']))

{

//Insert code

<?php


/*$host="localhost"; // Host name 

$username=""; // Mysql username 

$password=""; // Mysql password 

$db_name="test"; // Database name 

$tbl_name="test_mysql"; // Table name*/


// Connect to server and select database.

mysql_connect("localhost", "root", "")or die("cannot connect"); 

mysql_select_db("test")or die("cannot select DB");


// Get values from form 

$empno=$_POST['empno'];

$empname=$_POST['empname'];

$desig=$_POST['desig'];


// Insert data into mysql 

$sql="INSERT INTO emp(empno, empname, desig)VALUES('$empno', '$empname', '$desig')";

$result=mysql_query($sql);


// if successfully insert data into database, displays message "Successful". 

if($result){

echo "Successful";

echo "<BR>";

echo "<a href='insert.php'>Back to main page</a>";

}


else 

{

echo "ERROR";

}

// close connection 

mysql_close();

?>

}

else if (isset($_POST['delete']))

{

//Delete code

<?php

$qs=$_REQUEST['seno'];

mysql_connect("localhost","root","");

mysql_select_db("test");

mysql_query("delete from emp where sno=$qs");

header("location:getrec.php");

?>

/*<script>

function delrec(sno)

{

rv=confirm("u want to delete");

if(rv==true)

{

location="delete.php?seno="+sno;

}

}

</script>

*/

}


<input type="hidden" name="insert" value="true" />


<input type="hidden" name="delete" value="true" />

Edited by Roger, 18 August 2011 - 09:38 PM.


#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Is this the whole code?
I mean it seem to be some code missing.
Plus their some php code outside php tags, so it will simply be print out like normal html code

Why don't you show us all your code and it will be more easy to help you




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users