Jump to content

delete and update button problems!


spanner206
Go to solution Solved by Ch0cu3r,

Recommended Posts

hi ive been trying to make these buttons for a while now and its starting to get to the point were im considering lobbing the computer off a bridge and i wouldnt care cause well ile enjoy doing it, but to be fair its a work computer and i might get told off, but yh ive been having problems with this for a couple of days now what im trying to do is put an update record button and a delete record button into my table so you dont have to go into the database to change the records, at the start i had quite a lot of errors and i think ive solved them all and hopefully im down the last few but i really have no clue on how to fix these i have had help over the past few days via this site and its really worked so hopefully this is the last time, aswell as that im very inexperianced in php but snail pace speed getting better.

 

heres the code.

 
<?php
 
$con = mysqli_connect("localhost","root","","lcm");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//updates table.
if (isset($_POST["update"]))
{
$name    = mysql_real_escape_string($_POST['name']);
$address = mysql_real_escape_string($_POST['address']);
$hidden  = mysql_real_escape_string($_POST['hidden']);
 
$UpdateQuery = "UPDATE tbl_contactinfo SET Name='$_POST[name]', Address='$_POST[address]' WHERE name='$_POST[hidden]'";
mysqli_query($UpdateQuery, $con);
}
if (isset($_POST['delete']))
{
$name    = mysql_real_escape_string($_POST['name']);
$address = mysql_real_escape_string($_POST['address']);
$hidden  = mysql_real_escape_string($_POST['hidden']);
 
$DeleteQuery =  "DELETE FROM tbl_contactinfo WHERE Name='$_POST[hidden]'";
mysqli_query($DeleteQuery, $con);
}
//selects the table.
$sql = "Select * from tbl_contactinfo";
if ($result = mysqli_query($con, $sql))
{
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Address</th>
</tr>";
 
while($row = mysqli_fetch_array($result))
{
echo '<form action="index1.php" method="post">';
echo '<tr>';
echo '<td><input type="text" name="name" value="' . $row['Name'] . '" /></td>'; 
echo '<td><input type="text" name="address" value="' . $row['Address'] . '" /></td>';
echo '<td><input type="hidden" name="hidden" value="' . $row['ID'] . '" /></td>'; 
echo '<td><input type="submit" name="update" value="update" /></td>';
echo '<td><input type="submit" name="delete" value="delete" /></td>';
echo '</tr>';
echo '</form>';
}
echo "</table>";
 
}
?>
<html>
<body>
 
<form action="insert.php" method="post">
Name: <input type="text" name="Name">
Address: <input type="text" name="Address">
<input type="submit">
</form>
 
</body>
</html>
 
 
and the error messages.
 
update error
( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\Index1.php on line 17 Call Stack # Time Memory Function Location 1 0.0000 149320 {main}( ) ..\Index1.php:0 2 0.0000 162976 mysqli_query ( )

..\Index1.php:17

 

and the delete error

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\Index1.php on line 26 Call Stack # Time Memory Function Location 1 0.0000 149448 {main}( ) ..\Index1.php:0 2 0.0000 163136 mysqli_query ( ) ..\Index1.php:26

 

if anyone knows whats wrong please post as im loosing hair as i speak :D

Link to comment
Share on other sites

Since you are using mysqli, you need to use it consistently

 

 

 

$name    = mysql_real_escape_string($_POST['name']);
$address = mysql_real_escape_string($_POST['address']);
$hidden  = mysql_real_escape_string($_POST['hidden']);

 

Note the mysqli versions also need the $con parameter (in the first position)

Link to comment
Share on other sites

  • Solution

Also you're trying to update/delete records where Name=$_POST[hidden]. The form field named hidden contains the records ID value

echo '<td><input type="hidden" name="hidden" value="' . $row['ID'] . '" /></td>'; 

The queries need to update/delete records where the ID column equals to $_POST[hidden]. 

<?php
 
$con = mysqli_connect("localhost","root","","lcm");
// Check connection
if (mysqli_connect_errno())
{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//updates table.
if (isset($_POST["update"]))
{
	$name    = mysqli_real_escape_string($con, $_POST['name']);
	$address = mysqli_real_escape_string($con, $_POST['address']);
	$hidden  = mysqli_real_escape_string($con, $_POST['hidden']);
 
	$UpdateQuery = "UPDATE tbl_contactinfo SET Name='$name', Address='$address' WHERE ID='$hidden'";
	mysqli_query($con, $UpdateQuery);
}

if (isset($_POST['delete']))
{
	$hidden  = mysqli_real_escape_string($con, $_POST['hidden']);
 
	$DeleteQuery =  "DELETE FROM tbl_contactinfo WHERE ID='$hidden'";
	mysqli_query($con, $DeleteQuery);
}

//selects the table.
$sql = "Select * from tbl_contactinfo";
if ($result = mysqli_query($con, $sql))
{
	echo "<table border='1'>
	<tr>
	<th>Name</th>
	<th>Address</th>
	</tr>";
	 
	while($row = mysqli_fetch_array($result))
	{
		echo '<form action="index1.php" method="post">';
		echo '<tr>';
		echo '<td><input type="text" name="name" value="' . $row['Name'] . '" /></td>'; 
		echo '<td><input type="text" name="address" value="' . $row['Address'] . '" /></td>';
		echo '<td><input type="hidden" name="hidden" value="' . $row['ID'] . '" /></td>'; 
		echo '<td><input type="submit" name="update" value="update" /></td>';
		echo '<td><input type="submit" name="delete" value="delete" /></td>';
		echo '</tr>';
		echo '</form>';
	}
	echo "</table>";
}
?>
<html>
<body>
 
<form action="insert.php" method="post">
Name: <input type="text" name="Name">
Address: <input type="text" name="Address">
<input type="submit" name="submit">
</form>
 
</body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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