当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Can't change the MySQL Variable with a simple HTML form

问题描述:

when I try this code, I get an error like;

Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/28/10527728/html/TestSite/page.php on line 20

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/content/28/10527728/html/TestSite/page.php on line 20

Warning: mysql_query() expects parameter 2 to be resource, null given in /home/content/28/10527728/html/TestSite/page.php on line 21

Could not update data: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Code:

<?php

ob_start();

session_start();

if(!isset($_SESSION['Username'])){

header("Location: page.php");

}

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

{

$Memberid = $_POST['Memberid'];

$Username = $_POST['Username'];

$sql = "UPDATE members ".

"SET Username = $Username ".

"WHERE Memberid = $Memberid" ;

mysql_select_db($finalform);

$retval = mysql_query( $sql, $conn );

if(! $retval )

{

die('Could not update data: ' . mysql_error());

}

echo "Updated data successfully\n";

mysql_close($conn);

}

else

{

?>

The HTML for to edit the Username of the members table, in databse finalform;

 <form method="post" action="<?php $_PHP_SELF ?>">

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

<tr>

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

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

</tr>

<td width="100"> </td>

<td>

<input name="update" type="submit" id="update" value="Update">

</td>

</tr>

</table>

</form>

<?php

} //Curly bracket close from the else from the begining

?>

</body>

</html>

网友答案:

You're missing the mysql_connect() function. You're closing $conn so let's use that:

$conn = mysql_connect('localhost', 'user', 'pass');

That being said, you should really try to use PDO or a similar alternative

网友答案:
  $conn = mysql_connect( server, username, password );

i.e. :

  $conn = mysql_connect( 'localhost', 'userName', 'yourPassword' );

I agree try PDO, for a solution with immediate effect try my solution.

分享给朋友:
您可能感兴趣的文章:
随机阅读: