...

View Full Version : Php issue - updating table in mysql



jonny1990
01-05-2012, 04:18 PM
Hi All, simply trying to make a update in content for editing a user information: Here is my code:

I seem to believe it is not working because i havent applied it to a variable in my update_ac.php

So here goes:

EDIT.PHP



<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


echo("Logged In As: $UserName");
echo "<br />";
echo("We are editing Data for ID: $adminid");
echo "<br />";
echo "<a href=test.php>Go back to panel</a>";

$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM admin WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Main Content</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" size="15"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Page</title>
</head>

<body>
<h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
</body>
</html>



UPDATE_AC.PHP



<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database
$sql="UPDATE admin SET name='$name', mainContent='$mainContent' WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());;

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='test.php'>Back to panel</a>";
}

else {
echo "ERROR";
}

?>






I must state that I am not getting a error, therefore must be something to do with update_ac.php.

Everything works successfull but the table does not upload: I seem to believe it could be because i havent assigned it to a _POST variable for each form field.

devinmaking
01-05-2012, 04:50 PM
Hi All, simply trying to make a update in content for editing a user information: Here is my code:

I seem to believe it is not working because i havent applied it to a variable in my update_ac.php

So here goes:

EDIT.PHP



<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


echo("Logged In As: $UserName");
echo "<br />";
echo("We are editing Data for ID: $adminid");
echo "<br />";
echo "<a href=test.php>Go back to panel</a>";

$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM admin WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Main Content</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" size="15"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Page</title>
</head>

<body>
<h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
</body>
</html>



UPDATE_AC.PHP



<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database
$sql="UPDATE admin SET name='$name', mainContent='$mainContent' WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());;

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='test.php'>Back to panel</a>";
}

else {
echo "ERROR";
}

?>






I must state that I am not getting a error, therefore must be something to do with update_ac.php.

Everything works successfull but the table does not upload: I seem to believe it could be because i havent assigned it to a _POST variable for each form field.

You will not get any errors as there are none.

You have not caught what is being sent from the form.

You will needed to catch the post variables and then input into the database.

Another thing is you are not making variables that are being input into the database safe.

You need to mysql escape them, also i would advice trim and htmlentities as well as making sure that the id is infact and int

jonny1990
01-06-2012, 11:10 AM
Can anyone else give me a hand still stuck on this issue!

Here i have entered teh _POST so the forms get sent to a variable, but still no luck!

update_ac.php




<?php
$host="localhost"; // Host name
$username="jonathon_admin"; // Mysql username
$password="hello123"; // Mysql password
$db_name="jonathon_admin1"; // Database name
$tbl_name="admin"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database

$firstName = $_POST["name"];
$mainText = $_POST["mainContent"];

$sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());;

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='test.php'>Back to panel</a>";
}

else {
echo "ERROR";
}

?>

jonny1990
01-06-2012, 11:16 AM
You will not get any errors as there are none.

You have not caught what is being sent from the form.

You will needed to catch the post variables and then input into the database.

Another thing is you are not making variables that are being input into the database safe.

You need to mysql escape them, also i would advice trim and htmlentities as well as making sure that the id is infact and int

Thanks, but this is not helping me learn, i understand the first two points, atleast if i am trying to do a step by step then i will understand, in regards to the mysql escape, i have not covered this therefore will need to take this into account when i deal with this...

jonny1990
01-06-2012, 05:26 PM
Unfortunately i still havent managed to resolve this issue, anyone have any pointers?

jonny1990
01-06-2012, 05:35 PM
I have found a result guys, but not to complete one, when i change this line of code:

$sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='1'";

changing the ID to a fix ID it works fine and updates. So this must be a variable issue?

melloorr
01-06-2012, 06:30 PM
Post all the code you currently have

jonny1990
01-06-2012, 07:24 PM
obviously i have loads more but nothing is linked to the edit.php apart from probs the SESSION

These are the only two main focus pages, everything else is not related to this

melloorr
01-06-2012, 07:46 PM
$sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='$id'";

You have not defined the variable $id anywhere. (Well i did not see it.)

jonny1990
01-06-2012, 07:52 PM
yep it has been defined

melloorr
01-06-2012, 07:54 PM
yep it has been defined

Where? if it is in a separate php file, then you will need to use include

jonny1990
01-06-2012, 07:58 PM
<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


echo("Logged In As: $UserName");
echo "<br />";
echo("We are editing Data for ID: $adminid");
echo "<br />";
echo "<a href=test.php>Go back to panel</a>";

$id=$_GET['id'];

melloorr
01-06-2012, 08:00 PM
Yes, and I think you will find, that it is NOT in update_ac.php, so it cannot be used........

jonny1990
01-06-2012, 08:05 PM
ok, but i re-declared it in my updated version, i will take another look, should this work?

melloorr
01-06-2012, 08:11 PM
If you had a hidden field in your form,

<input type="hidden" name="ID" value="$id">

then in update_ac.php

you can use


$id = $_POST['ID'];

jonny1990
01-06-2012, 11:39 PM
I knew i could count on you for a good answer mate, totally forgot i had it hidden in my form... cheers mate, top guy! +1 again for your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum