Jump to content

update of mysql does not work


gwolff2005

Recommended Posts

Hi I have the file "pback.php" which displays a mysql table.

One row allows me to update the data.

If I press "update it leads me to update.php, but there I just get white fields instead of the data from the id requested.

If I type anything in the field and press confirm it opens update_ac but just says error.

What do I do wrong?

Thanks!

pback.php

<?php
$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="sp_users"; // 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");

//$sql="SELECT * FROM $tbl_name ORDER BY user_id";
$sql="SELECT * from sp_users,sp_schools where sp_users.user_id=sp_schools.school_id ORDER BY school_name";
$result=mysql_query($sql);
$num_rows=mysql_num_rows($result);
?>
<style type="text/css">
<!--
.style2 {font-weight: bold}
.style3 {
font-family: Arial, Helvetica, sans-serif;
color: #000000;
}
.style10 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #000000; }
#Layer1 {
position:absolute;
left:803px;
top:36px;
width:65px;
height:28px;
z-index:1;
}
#Layer2 {
position:absolute;
left:707px;
top:19px;
width:143px;
height:39px;
z-index:1;
}
-->
</style>

<title>User overview</title>
</html>
<div id="Layer2">
  <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="foo" value="<?= $foo ?>" />
<input type="submit" name="submit" value="Refresh" />
</form></div>
<table width="779" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#996600">
<tr>
<td width="777">
  
    <div align="left">
      <table width="779" border="1" cellspacing="0" cellpadding="3">
        <tr>
          <td colspan="6" align="center"><div align="center" class="style1 style3"><strong>SchoolPorta.com Users / Total: <?php echo $num_rows ?></strong></div></td>
          </tr>
        <tr>
          <td width="342" align="center"><span class="style2">school</span></td>
          <td width="62" align="center"><span class="style2">Name</span></td>
          <td width="104" align="center"><span class="style2">Lastname</span></td>
          <td width="130" align="center"><span class="style2">Email</span></td>
   		  <td width="64" align="center"><span class="style2">Update</span></td>
        </tr>
        <?php
while($rows=mysql_fetch_array($result)){
?>
        <tr>
          <td><span class="style10"><? echo $rows['school_name']; ?></span></td>
	  <td><span class="style10"><? echo $rows['user_first_name']; ?></span></td>
          <td><span class="style10"><? echo $rows['user_surname']; ?></span></td>
          <td><span class="style10"><a href="mailto:<?php echo $rows['user_login']; ?>"><?php echo $rows['user_login']; ?></a></span></td>
          <td align="center"><a href="update.php?id=<? echo $rows['user_id']; ?>" class="style10">update</a></td>
        </tr>
        <?php
}
?>
      </table>
    </div></td>
</tr>
</table>

<div align="left">
  <p>    </p>
  <p> </p>
  <p> </p>
  <p>
    <?php
mysql_close();
?>
  </p>
</div>

 

update.php

<?php

$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
//$tbl_name="sp_users"; // 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");

// get value of id that sent from address bar
$id=$_GET['user_id'];

?>
<?php
$sql="SELECT * from sp_users,sp_schools where sp_users.user_id=sp_schools.school_id";
$result=mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_query());
?>
<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"><div align="center" class="style1">Update data in mysql </div></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"><span class="style7">Name</span></td>
<td align="center"><span class="style7">Lastname</span></td>
<td align="center"><span class="style7">Email</span></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['user_first_name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['user_surname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['user_login']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['user_id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection
mysql_close();

?>
</body>
</html>

 

update_ac.php

<?php
$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="sp_users"; // 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
$sql="UPDATE $tbl_name SET name='$user_name', lastname='$user_lastname', email='$email' WHERE id='$user_id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='pback.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/232038-update-of-mysql-does-not-work/
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.