Forum Moderators: coopster

Message Too Old, No Replies

Problem executing mysql query

Help

         

dangeh

6:12 am on Sep 14, 2007 (gmt 0)

10+ Year Member



I am getting this message "Can't execute insert query." I believe the problem is coming from line 35. My login data table only has 2 strings, loginName and LoginTime. the book I am doing this from tells me both should be Primary Keys. my server only allows me to pick one as a Primary Key. I picked loginName. I am new at all this... and it is just boggleing my mind. If anyone can help me through this, it would be much appreciated.

case "login":
$cxn = mysqli_connect($host, $user, $passwd,$dbname)
or die ( "Couldn't connect to server. ") ;

$sql = "SELECT loginName FROM member
WHERE loginName='$_POST[fusername] ' " ;
$result = mysqli_query($cxn, $sql)
or die ("Couldn't execute query.") ;
$num = mysqli_num_rows($result);
if ($num > 0) // login name was found
{
$sql = "SELECT loginName FROM member
WHERE loginName='$_POST[fusername] '
AND password=md5('$_POST[fpassword]') " ;
$result2 = mysqli_query($cxn,$sql)
or die("Couldn't execute query 2.");
$num2 = mysqli_num_rows($result2);
if ($num2 > 0) // password is correct
{
$_SESSION['auth']="yes";
$logname=$_POST['fusername'];
$_SESSION['logname'] = $logname;
$today = date("Y-m-d h:i:s");
$sql = "INSERT INTO login (loginName,loginTime)
VALUES ('$logname','$today')";
$result = mysqli_query($cxn,$sql)
(35) or die("Can't execute insert query.");
header("Location: member_page.php");
}
else // password is not correct
{
$message="The Login Name, '$_POST[fusername]'
exists, but you have not entered the
correct password! Please try again.<br />";
include("login_form.inc");
}
}
elseif ($num == 0) // login name not found
{
$message = "The Login Name you entered does not
exist! Please try again.<br />";
include("login_form.inc");
}
break;

jatar_k

3:13 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$sql = "INSERT INTO login (loginName,loginTime) VALUES ('$logname','$today')";

have you tried echoing the query to see if it looks correct?

also it is great to put or die on your query exec but what you really need and want is this

$result = mysqli_query($cxn,$sql) or die("Can't execute insert query: " . mysqli_error());

this will return the actual error from mysql and give you some useful data

dangeh

3:36 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



I dont know what you mean by echoing, but I added that other line and this is the error it gave me
this is line 39

or die("Can't execute insert query: " . mysqli_error());

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/music/public_html/login.php on line 39
Can't execute insert query:

[edited by: dangeh at 3:42 pm (utc) on Sep. 14, 2007]

jatar_k

3:40 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> echoing

meaning use echo [php.net]

like so
$sql = "INSERT INTO login (loginName,loginTime) VALUES ('$logname','$today')";
echo '<p>the query looks like this: ',$sql;

I'm liking the "improved" mysql functions less and less

so you need to pass it the link/connection, as mentioned on the manual page
[php.net...]

$result = mysqli_query($cxn,$sql) or die("Can't execute insert query: " . mysqli_error($cxn));

the manual is your best friend

dangeh

4:02 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



Buddy... I thank you!
It showed me the error... told me I was missing a data base, I added the data base and wa la!
I thank you again!