Jump to content

PHP form won't work


adamjones

Recommended Posts

OK. So I have a PHP form, to update my users information. It goes like so;

 

editadmin.php (The first page an admin goes to, to update another users info) :-

 

<?php

$host="localhost";

$username="wowdream_domaine";

$password="mypassword";

$db_name="wowdream_domaine";

$tbl_name="members";

 

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);

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">

<tr>

<td>

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

<tr>

</tr>

 

<tr>

<td align="center"><strong>Username</strong></td>

<td align="center"><strong>Password</strong></td>

<td align="center"><strong>Rank</strong></td>

<td align="center"><strong>Update</strong></td>

<td align="center"><strong>Delete</strong></td>

</tr>

<?php

while($rows=mysql_fetch_array($result)){

?>

<tr>

<td><? echo $rows['username']; ?></td>

<td>********</td>

<td><? echo $rows['access']; ?></td>

 

<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>

<td align="center"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td>

</tr>

<?php

}

?>

</table>

</td>

</tr>

</table>

<?php

mysql_close();

?>

 

update.php (This displays an update form, depending on the user selected and their ID) :-

 

<?php

$host="localhost";

$username="wowdream_domaine"; 

$password="mypassword";

$db_name="wowdream_domaine"; 

$tbl_name="members";

 

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$id=$_GET['id'];

 

$sql="SELECT * FROM $tbl_name WHERE id='$id'";

$result=mysql_query($sql);

 

$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> </td>

<td colspan="3"><strong>Update data in mysql</strong> </td>

</tr>

<tr>

<td align="center"> </td>

<td align="center"> </td>

<td align="center"> </td>

<td align="center"> </td>

</tr>

<tr>

<td align="center"> </td>

<td align="center"><strong>Username</strong></td>

<td align="center"><strong>Password</strong></td>

<td align="center"><strong>Access</strong></td>

</tr>

<tr>

<td> </td>

<td align="center"><input name="username" type="text" id="username" value="<? echo $rows['username']; ?>" size="15"></td>

<td align="center"><input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" size="15"></td>

<td align="center"><input name="access" type="text" id="access" value="<? echo $rows['access']; ?>" size="15"></td>

</tr>

<tr>

<td> </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> </td>

</tr>

</table>

</td>

</form>

</tr>

</table>

 

<?

mysql_close();

?>

 

admin_ac.php (The information is posted here, and is updated in the database) :-

 

<?php

$host="localhost";

$username="wowdream_domaine";

$password="mypassword";

$db_name="wowdream_domaine";

$tbl_name="members";

 

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'";

$result=mysql_query($sql);

 

if($result){

echo "Successful";

echo "<BR>";

echo "<a href='editadmin.php'>View result</a>";

}

 

else {

echo "ERROR";

}

 

?>

 

 

OK. The problem is, it does everything fine, except when I change the info in the form, instead of updating, it echo's my error - :(

Could anyone work out what's wrong with it?

 

Cheers,

Adam.

Link to comment
https://forums.phpfreaks.com/topic/127101-php-form-wont-work/
Share on other sites

OK, another thing.

 

Where are your variables declared????

 

$sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'";

 

'$username' ????

'$password' ????

'$access' ????

'$id' ????

 

You variables are emtpy so nothing gets updated!!!! That's why you get error!!!!

 

Link to comment
https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657455
Share on other sites

Ok. Sorry guys-

 

editadmin.php -

 

<?php
$host="localhost"; 
$username="wowdream_domaine";
$password="mypassword";
$db_name="wowdream_domaine";
$tbl_name="members";

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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
</tr>

<tr>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Rank</strong></td>
<td align="center"><strong>Update</strong></td>
<td align="center"><strong>Delete</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['username']; ?></td>
<td>********</td>
<td><? echo $rows['access']; ?></td>

<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update[/url]</td>
<td align="center"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete[/url]</td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

 

Update.php -

 

<?php
$host="localhost";
$username="wowdream_domaine";
$password="mypassword";
$db_name="wowdream_domaine";
$tbl_name="members";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$id=$_GET['id'];

$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$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> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Access</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="username" type="text" id="username" value="<? echo $rows['username']; ?>" size="15"></td>
<td align="center"><input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" size="15"></td>
<td align="center"><input name="access" type="text" id="access" value="<? echo $rows['access']; ?>" size="15"></td>
</tr>
<tr>
<td> </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> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?
mysql_close();
?>

 

Update_ac.php -

 

<?php
$host="localhost";
$username="wowdream_domaine";
$password="mypassword";
$db_name="wowdream_domaine";
$tbl_name="members";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'";
$result=mysql_query($sql);

if($result){
echo "Successful";
echo "<BR>";
echo "<a href='editadmin.php'>View result[/url]";
}

else {
echo "ERROR";
}

?>

 

And in regards to

1st - When you post code please place it between [ code] and [/ code]

2nd - change:

else {
echo "ERROR";
}

with

else {
echo "ERROR:" . mysql_error();
}

Then tell us what it says.

 

- I have done this now, but it's not echoing an error.

Link to comment
https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657515
Share on other sites

<a href="update.php?id=<? echo $rows['id']; ?>">update</a>

 

Note that you're using short tags there. Check the generated HTML source to verify that id actually has a value. If not, edit that code to:

 

<a href="update.php?id=<?php echo $rows['id']; ?>">update</a>

 

Other than that, it's likely one or more of your SQL queries is failing. You can check that easily enough bu using rational error trapping/display.  For example, change instances of:

 

$result=mysql_query($sql);

 

to:

$result=mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $sql);

Link to comment
https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657561
Share on other sites

Nope- Still not working!

 

Can you confirm that your HTML form does actually include a value for the id in the clickable links?

 

Please also identify each/any error message you got after making the changes I suggested.

 

'not working' isn't helping us help you.

Link to comment
https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657593
Share on other sites

You're file "Update_ac.php" is not complete you are missing couple of things.

 

This is wrong:

<?php
$host="localhost";
$username="wowdream_domaine";
$password="mypassword";
$db_name="wowdream_domaine";
$tbl_name="members";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'";
$result=mysql_query($sql);

if($result){
echo "Successful";
echo "<BR>";
echo "<a href='editadmin.php'>View result[/url]";
}

else {
echo "ERROR";
}

?>

 

This is right:

 

<?php
$host="localhost";
$username="wowdream_domaine";
$password="mypassword";
$db_name="wowdream_domaine";
$tbl_name="members";

mysql_connect("$host", "$username", "$password") or die ("cannot connect"); 
mysql_select_db("$db_name") or die ("cannot select DB");

$id = $_POST["id"];
$username1 = $_POST["username"];
$password1 = $_POST["password"];
$access1 = $_POST["access"];

$sql="UPDATE $tbl_name SET username='$username1', password='$password1', access='$access1' WHERE id='$id'";
$result=mysql_query($sql) or die (mysql_error());
$num=mysql_num_rows($result);

if($num == 1){
echo "Successful";
echo "<br />";
echo "<a href='editadmin.php'>View result[/url]";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657597
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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