Jump to content

Php failing to execute query.


abch624

Recommended Posts

Hi there,

 

I cannot execute my mysql query. I am new to php and I dont have a clue why this is happening. All the code I am using is bellow:

 

join.php

<?php
include $_SERVER['DOCUMENT_ROOT'].'/mini.php';

switch($_REQUEST['req']){
case "process":

   // Validate all required fields were posted
   if(!$_POST['first_name'] ||
      !$_POST['last_name'] ||
      !$_POST['email_address'] ||
      !$_POST['email_address2'] ||
      !$_POST['username'] ||
      !$_POST['password'] ||
      !$_POST['password2'] ||
      !$_POST['bio']){
        
         $error = true;
         $errors .= "<strong>Form Input Errors:".
                    "</strong>\n\n";
        
         if(!$_POST['first_name']){
            $errors .= "Missing First Name\n";
         }
        
         if(!$_POST['last_name']){
            $errors .= "Missing Last Name\n";
         }
       
         if(!$_POST['email_address']){
            $errors .= "Missing Email Address\n";
            $email_error = true;
         }
        
         if(!$_POST['email_address2']){
            $errors .= "Missing Email Address".
                       "Verification\n";
            $email_error = true;
         }
        
         if(!$_POST['username']){
            $errors .= "Missing Username\n";
         }
        
         if(!$_POST['password']){
            $errors .= "Missing Password\n";
            $password_error = true;
         }
        
         if(!$_POST['password2']){
            $errors .= "Missing Password Verification\n";
            $password_error = true;
         }
        
         if(!$_POST['bio']){
            $errors .= "Missing Information About ".
                       "Yourself\n";
         }
   }
  
   // If both emails were posted, validate they match.
   if($email_error == false){
         if($_POST['email_address'] !=
                  $_POST['email_address2']){
            $error = true;
            $errors .= "Email addresses do not match!\n\n";
            $email_error = true;
         }
   }
  
   // If both passwords were posted, validate they match.
   if($password_error == false){
         if($_POST['password'] != $_POST['password2']){
            $error = true;
            $errors .= "Passwords do not match!\n\n";
            $password_error = true;
         }
   }
    /*
   if($email_error == false){
      // Verify if email address has been used already.
      $ecount = mysql_result(mysql_query("SELECT COUNT(*)
                     AS ecount FROM members
                     WHERE email_address =
                     '{$_POST['email_address']}'"),0);
    
      // If email exists, generate error and message.  
      if($ecount > 0){
         $error = true;
         $errors .= "This email address has already ".
                    "been used ".
                    "please choose another.\n\n";
      }
   }

   // Verify if username already exists.
   $ucount = mysql_result(mysql_query("SELECT COUNT(*)
                  AS ucount FROM members
                  WHERE username =
                  '{$_POST['username']}'"),0);

   // If username exists, generate error and message.  
   if($ucount > 0){
      $error = true;
      $errors .= "Username already exists, ".
                 "please choose another.\n\n";
   }
  
   // If $error is TRUE, then include the signup form
   // and display the errors we found.
  */
  
   if($error == true){
      $errors = nl2br($errors);
      include $_SERVER['DOCUMENT_ROOT'].
              '/sign_up.html';
      footer();
      exit();
   }
  
   
   $user="root";
$host="localhost";
$password="";
$database="venudatabase";
   
   $cxn = mysqli_connect($host,$user,$passwd,$dbname)
       or die ("couldn't connect to the database");

   // All checks have passed, insert user in database
   $sql = @mysql_query("INSERT INTO members (first_name,
                     last_name, email_address, signup_date,
                     bio, username, password)
                     VALUES ('$_POST[first_name]', '$_POST[last_name]',
                     '$_POST[email_address]', now(), '$_POST[bio]',
                     '$_POST[username]', '".md5($_POST[password])."')") or die ("NO");
  
   // All checks have passed, insert user in database
  
   // Email user
  
   // Email Admin
  
   // That's it! Done!
break;   

      default:
      include $_SERVER['DOCUMENT_ROOT'].
              '/sign_up.html';
   break;
}
?>

 

layout.php

<?php
function myheader($ptitle){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<table width="100%" border="2" cellpadding="0"
cellspacing="0" bordercolor="#000000">
  <tr>
    <td colspan="3"> </td>
  </tr>
  <tr>
    <td> </td>
    <td>
    <!-- End Header and Begin Content -->
<?php
} // close myheader()
function footer(){
?>
<!-- End Content and Begin Footer -->
    </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</body>
</html>
}  //close footer()
?>

 

sign_up.html

<p><font size="4" face="Verdana, Arial, Helvetica, sans-serif"><strong>Become
  a Member!</strong></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Join our website
  and enjoy the benefits of becoming a member!</font></p>
<?php
if($errors){
echo "<p align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#FF0000\">$errors</font></p>\n";
}
?>
<form method="post" action="/join.php">
  <table width="50%" border="1" align="center" cellpadding="4" cellspacing="0">
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">First Name</font></td>
      <td width="179" align="left" valign="top"><input name="first_name" type="text" id="first_name" value="<?=$_POST['first_name'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Last Name</font></td>
      <td align="left" valign="top"><input name="last_name" type="text" id="last_name" value="<?=$_POST['last_name'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email Address</font></td>
      <td align="left" valign="top"><input name="email_address" type="text" id="email_address" value="<?=$_POST['email_address'];?>"></td>
    </tr>
    <tr>
      <td align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Verify Email Address</font></td>
      <td align="left" valign="top"><input name="email_address2" type="text" id="email_address3" value="<?=$_POST['email_address2'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Desired Username</font></td>
      <td align="left" valign="top"><input name="username" type="text" id="username" value="<?=$_POST['username'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Password</font></td>
      <td align="left" valign="top"><input name="password" type="password" id="password" value="<?=$_POST['password'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Password Again</font></td>
      <td align="left" valign="top"><input name="password2" type="password" id="password2" value="<?=$_POST['password2'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Tell us about yourself!</font></td>
      <td align="left" valign="top"><textarea name="bio"><?=$_POST['bio'];?></textarea></td>
    </tr>
    <tr>
      <td align="left" valign="top"> </td>
      <td align="left" valign="top"><input name="req" type="hidden" id="req" value="process">
        <input type="submit" name="Submit" value="Submit Information!"></td>
    </tr>
  </table>
</form>

 

index.html

<?php
include $_SERVER['DOCUMENT_ROOT'].'/layout.php';
include $_SERVER['DOCUMENT_ROOT'].'/mini.php';

myheader("First");
include $_SERVER['DOCUMENT_ROOT'].'/sign_up.html';
footer();
?>

 

layout.php

<?php
function myheader($ptitle){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<table width="100%" border="2" cellpadding="0"
cellspacing="0" bordercolor="#000000">
  <tr>
    <td colspan="3"> </td>
  </tr>
  <tr>
    <td> </td>
    <td>
    <!-- End Header and Begin Content -->
<?php
} // close myheader()
function footer(){
?>
<!-- End Content and Begin Footer -->
    </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</body>
</html>
<?php
}  //close footer()
?>

 

I run the index.html file and the data I enter in the form is not updated to the database...

Please help.

Link to comment
Share on other sites

just post the part that has an error?

 

It aint' gonna error cause the query is messed up

Rules around here are dont' post sql errors if you are supressing errrors change all queries to

mysql_query($q) or die(mysql_error()."<br />".$q);

 

and replace $q with what is the query, and then report the errors.

 

Link to comment
Share on other sites

just post the part that has an error?

The part which gives me an error is this sql statement, when the code tries to put the data I provided via the form, there is a problem and I dont get it why...

 

// All checks have passed, insert user in database

  $sql = @mysql_query("INSERT INTO members (first_name,

                    last_name, email_address, signup_date,

                    bio, username, password)

                    VALUES ('$_POST[first_name]', '$_POST[last_name]',

                    '$_POST[email_address]', now(), '$_POST[bio]',

                    '$_POST[username]', '".md5($_POST[password])."')") or die ("NO");

 

When I press the submit button prints "NO".

Link to comment
Share on other sites

[quote author=otuatail link=topic=166033.msg730621#msg730621 date=1193959608]
instead of $sql = @mysql_query(............)
seprate it into to lines create the $sql as a string

$sql = "INSERT INTO ........ ";
echo $sql;
$result = mysql_query($sql);  

You can see the SQL and send it to us. 
Are the fields of the correct type?


[/quote]

I did this and my new code would be
[code]$user="root";
$host="localhost";
$password="";
$database="venudatabase";
   
   $cxn = mysqli_connect($host,$user,$password,$database)
       or die ("couldn't connect to the database");

   // All checks have passed, insert user in database
   $sql = "INSERT INTO members (first_name,
                     last_name, email_address, signup_date,
                     bio, username, password)
                     VALUES ('$_POST[first_name]', '$_POST[last_name]',
                     '$_POST[email_address]', now(), '$_POST[bio]',
                     '$_POST[username]', '".md5($_POST[password])."')";

echo $sql;

$result = mysql_query($cxn,$sql);

 

The error I now get is INSERT INTO members (first_name, last_name, email_address, signup_date, bio, username, password) VALUES ('Zahid', 'Chaudhry', 'zc@statravelgroup.com', now(), 'me take care', 'abch624', '2c495e67f78495cca94b0b636f346de4')

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\join.php on line 139

and line 139 is "$result = mysql_query($cxn,$sql);[/code]".

 

Any clue why this is happening. Thanks

Link to comment
Share on other sites

and line 139 is "$result = mysql_query($cxn,$sql);

 

The arguments are the wrong way round.

 

And remove the error suppression while you are still having problems.

 

I tried that but it still does not work...

and what do mean by "And remove the error suppression while you are still having problems."

 

Thanks - Zahid

Link to comment
Share on other sites

If you seperated the creation of the SQL from the command as stated above. You can echo the statment. You can then copy it directly of the browser CTRL+C and if you are using phpMYadmin, paste it into their and see what error MYsql throws up. The query looks ok to me. remove the @ as well for a test.

 

 

Desmond.

 

Link to comment
Share on other sites

If you seperated the creation of the SQL from the command as stated above. You can echo the statment. You can then copy it directly of the browser CTRL+C and if you are using phpMYadmin, paste it into their and see what error MYsql throws up. The query looks ok to me. remove the @ as well for a test.

 

 

Desmond.

 

 

I tried doing what you said in MySql Console. and the sql statement works perfectly fine. The error I get now

is "Fatal error: Call to undefined function mysql_query() in C:\wamp\www\join.php on line 138"...

 

This is my first ever php script and giving me helllllll....

Link to comment
Share on other sites

If you are using "mysqli_connect()" then use mysqli.

 

Otherwise, use mysql_connect() with mysql_select_db();

 

Thanks I think you almost verified the problem but you were not clear...

The problem was that I was using mysql_querry(); instead of mysql_query($cxn,$sql);

 

Working now....

 

Thanks a lot.

Link to comment
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.