Jump to content

Recommended Posts

hello... there is a code beloew.. which i created... and on my local mashin it works and does not make any error.. but as soon as i upload it to server it give me error.. i do not understant why...

 

This is error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

and this is code of that page:

 

<?php
$login_username =  stripslashes($_POST['login_username']);
$login_password =  stripslashes($_POST['login_password']);

$oneday = 60 * 60 * 24 + time();
setcookie("firstschool_username", "$login_username", 
$oneday);
setcookie("firstschool_password", "$login_password", $oneday);
include ("includes/vars.php");
mysql_connect("$hostname",  "$username",  "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$query = "SELECT * FROM $login";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{

if ($login_username == $row[username] && $login_password == $row[password] )
{
echo "<meta http-equiv=\"REFRESH\" content=\"0;url=index.php\">";
}
else
{

echo "<meta http-equiv=\"REFRESH\" content=\"0; url=login.php\">";

exit;
}


}
?>

 

 

also here is the code of vars.php file which is included in this page ...

 


<?php

$hostname="localhost";
$username="schoge_admin";
$password="mypassword";
$database="schoge_firstschool";
$database2="forum";
$news="mynews";
$bulletin="mybulletin";
$navigation="menu";
$login="login";

?>

 

 

Thanks for your attention

 

Link to comment
https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/
Share on other sites

Looking at your code, it appears you are selecting every record and looping through them to check the id and pw.  This seems like a waste of resources.  What you want to do is do a SELECT statement but only return the record that matches.

 

$query = "SELECT * FROM `login` WHERE `username` = '$login_username' AND `password` = '$login_password'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo "Logged in, redirecting";
echo "<meta http-equiv=\"REFRESH\" content=\"3;url=index.php\">";
}
else {
echo "Wrong username or password";
echo "<meta http-equiv=\"REFRESH\" content=\"3; url=login.php\">";
}

 

Link to comment
https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465080
Share on other sites

....
while($row = mysql_fetch_array($result))
{

if ($login_username == $row[username] && $login_password == $row[password] )
{
echo "$login";
}
.....

 

 

yes on my local mashine it works and print just  a word login

 

 

but when on the server ... it prints nothing just an empty white page..

Link to comment
https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465123
Share on other sites

selecting statement but returning the record that matches solved the problem !!!

 

NOT IT WORKS.

 

but i wonder why?

 


<?php
$login_username =  stripslashes($_POST['login_username']);
$login_password =  stripslashes($_POST['login_password']);

$oneday = 60 * 60 * 24 + time();
setcookie('firstschool_username', '$login_username', $oneday);
setcookie('firstschool_password', '$login_password', $oneday);
include ("includes/vars.php");
mysql_connect("$hostname",  "$username",  "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$query = "SELECT * FROM `login` WHERE `username` = '$login_username' AND `password` = '$login_password'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo "Logged in, redirecting";
echo "<meta http-equiv=\"REFRESH\" content=\"3;url=index.php\">";
}
else {
echo "Wrong username or password";
echo "<meta http-equiv=\"REFRESH\" content=\"3; url=login.php\">";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465128
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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