Jump to content

mySQL 'update' not working..


finkyfeke

Recommended Posts

Hi,

 

I'm setting up a website on which you have to pay for membership. I allow the user to register with an 'elec' field in the database as '0'. The 'elec' field is '0' when they haven't paid and '1' when they have.

 

I've tested it and the login/register scripts work fine but i cant get it to change 'elec' to '1'. I use a separate script to buy elec, as shown below.

 

<?

// members page

session_start();

if ( empty( $username ) ) {

    include 'notloggedin.html';

} else {

$dbhost = "fdb1.awardspace.com";
$dbname = "ltstrings_db";
$dbuser = "ltstrings_db";
$dbpass = "********";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$buy = 'UPDATE `users` SET `elec` = \'1\' WHERE `username` = $username LIMIT 1';

if ($buy = 'TRUE')
{
echo 'You have successfully bought elec';
exit();
}
else
{
echo ' Unsuccessful...';
}


}

?>

 

Every time it shows the successful message but the 'elec' stays at '0'.

 

I'm new to PHP and SQL so it's likely I'm making an obvious mistake. Please help. Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/48978-mysql-update-not-working/
Share on other sites

Two things. 1) You forgot to put single quotes around the username search value ($username) and 2) you have $username inside the string using singe quotes. Variables are not interpreted inside single quoted strings. You would need to use magic quotes (double quotes) or append the variable to the string using a period.

 

Two solutions:

 

$buy = "UPDATE `users` SET `elec` = '1' WHERE `username` = '$username' LIMIT 1";

 

OR

 

$buy = 'UPDATE `users` SET `elec` = \'1\' WHERE `username` = \''.$username.'\' LIMIT 1';

 

Thanks a lot for your help, but now I'm getting the following error message with the login:

 

Warning: mysql_connect(): Too many connections in /home/www/ltstrings.awardspace.com/TEST/login.php on line 12

Could not connect: Too many connections

 

I've probably messed up with the login php as well so here it is

 

<?php

//Database Information

$dbhost = "fdb1.awardspace.com";
$dbname = "ltstrings_db";
$dbuser = "ltstrings_db";
$dbpass = "********";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

session_start();
$username = $_POST['username'];
$password = md5($_POST['password']);

$query = "select * from users where username='$username' and password='$password'";

$result = mysql_query($query);

if (mysql_num_rows($result) != 1) {
$error = 'Bad Login';
    echo 'Wrong Username and/or Password';
    include 'login.html';

} else {
    $_SESSION['username'] = '$username';
    include 'buyelec.php';
}

?>

 

Thanks again

http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html

 

If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients.

 

The number of connections allowed is controlled by the max_connections system variable. Its default value is 100. If you need to support more connections, you should restart mysqld with a larger value for this variable.

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.