Jump to content

simple php form not working


silverglade

Recommended Posts

Hi, I have a php form that I use to try to get matching data from the database that I put into the form. So if I enter date of birth 9-4-80 and first name Dave and lastname Smith. When I submit it the code should pull all of the matching terms out of the database and display. Now I get the following error when I submit the form.

 

Query: Resource id #2

Failed with 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 'Resource id #2' at line 1

 

any help Greatly appreciated. thank you!

 

 

here is the code

 

<?php 
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$host		= " ";
$database 	= " ";
$username 	= " ";
$password 	= " ";
$tbl_name   = "users";


$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());

if($conn)
{
mysql_select_db($database);
echo "connected to database!!";
} else {
	echo "failed to select database";
}




//include('bouncer.php');




//$currentUser = $_SESSION['myusername'];

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

{				 


$first = mysql_real_escape_string( $_POST['first']);
$last = mysql_real_escape_string( $_POST['last']);
$dob = mysql_real_escape_string( $_POST['dob']);



 	//THE SEARCH FUNCTION
$sql = mysql_query ( "SELECT * FROM users WHERE firstname  LIKE '%$first%' 
OR lastname LIKE '%$last%' OR dob  LIKE '%$dob%' ") or die(mysql_error());			 

  

$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() );

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}


while ($row = mysql_fetch_assoc($result)) {
    echo $row["firstname"];
    echo $row["lastname"];
    echo $row["dob"];
}





}//if(isset($_POST['submit']))
?>
<html>
<body>


<form action="login_success8.php" method="post">
  <p>
    <input type="text" name="first" size="20" /> 
  First name<br />
    <input type="text" name="last" size="20" />
    Last name<br />
    <input name="dob" type="text" size="20" id="dob" />
    Date of Birth<br />
    <input type="submit" name="submit" value="Search" />
    <input type="reset" value="Reset fields" />
  </p>
</form> 



</body>
</html>

Link to comment
Share on other sites

change to:

 

<?php
      $sql = mysql_query ("SELECT * FROM users WHERE firstname  LIKE '%" . $first . "%' 
OR lastname LIKE '%" . $last . "%' OR dob  LIKE '%" . $dob. "%' ") or die(mysql_error());	
?>

Link to comment
Share on other sites

You use mysql_query 2 times, that shouldn't work :)

 

<?php
//THE SEARCH FUNCTION
$sql = mysql_query ( "SELECT * FROM users WHERE firstname  LIKE '%$first%' 
OR lastname LIKE '%$last%' OR dob  LIKE '%$dob%' ") or die(mysql_error());			 

$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() );
?>

Link to comment
Share on other sites

thank you. The query is not supposed to run until I hit the form button, but I get the following error now.

 

No rows found, nothing to print so am exiting

 

Any more help I would appreciate it . thanks.

 

here is the newcode

 

<?php 
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$host		= " ";
$database 	= " ";
$username 	= " ";
$password 	= " ";
$tbl_name   = "users";

$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());

if($conn)
{
mysql_select_db($database);
echo "connected to database!!";
} else {
	echo "failed to select database";
}




//include('bouncer.php');




//$currentUser = $_SESSION['myusername'];

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

{				 


$first = mysql_real_escape_string( $_POST['first']);
$last = mysql_real_escape_string( $_POST['last']);
$dob = mysql_real_escape_string( $_POST['dob']);



 	//THE SEARCH FUNCTION
//$sql = mysql_query ( "SELECT * FROM users WHERE firstname  LIKE '%$first%' 
//OR lastname LIKE '%$last%' OR dob  LIKE '%$dob%'") or die(mysql_error());			

$result = mysql_query ("SELECT * FROM users WHERE firstname  LIKE '%" . $first . "%' 
OR lastname LIKE '%" . $last . "%' OR dob  LIKE '%" . $dob. "%' ") or die(mysql_error());	 

  

//$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() );

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}


while ($row = mysql_fetch_assoc($result)) {
    echo $row["firstname"];
    echo $row["lastname"];
    echo $row["dob"];
}





}//if(isset($_POST['submit']))
?>
<html>
<body>


<form action="login_success8.php" method="post">
  <p>
    <input type="text" name="first" size="20" /> 
  First name<br />
    <input type="text" name="last" size="20" />
    Last name<br />
    <input name="dob" type="text" size="20" id="dob" />
    Date of Birth<br />
    <input type="submit" name="submit" value="Search" />
    <input type="reset" value="Reset fields" />
  </p>
</form> 



</body>
</html>

 


Link to comment
Share on other sites

<?php 
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$host        = " ";
$database     = " ";
$username     = " ";
$password     = " ";
$tbl_name   = "users";

$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());

if($conn)
{
mysql_select_db($database);
echo "connected to database!!";
} else {
     echo "failed to select database";
    }




//include('bouncer.php');




//$currentUser = $_SESSION['myusername'];

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

{                 


$first = mysql_real_escape_string( $_POST['first']);
$last = mysql_real_escape_string( $_POST['last']);
$dob = mysql_real_escape_string( $_POST['dob']);

                 
              
         //THE SEARCH FUNCTION
//$sql = mysql_query ( "SELECT * FROM users WHERE firstname  LIKE '%$first%' 
//OR lastname LIKE '%$last%' OR dob  LIKE '%$dob%'") or die(mysql_error());            

$result = mysql_query ("SELECT * FROM users WHERE firstname  LIKE '%" . $first . "%' 
OR lastname LIKE '%" . $last . "%' OR dob  LIKE '%" . $dob. "%' ") or die(mysql_error());     
             
  

//$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() );

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}


while ($row = mysql_fetch_assoc($result)) {
    echo $row["firstname"];
    echo $row["lastname"];
    echo $row["dob"];
}


                

        
}else{ 
?>
<html>
<body>


<form action="login_success8.php" method="post">
  <p>
    <input type="text" name="first" size="20" /> 
  First name<br />
    <input type="text" name="last" size="20" />
    Last name<br />
    <input name="dob" type="text" size="20" id="dob" />
    Date of Birth<br />
    <input type="submit" name="submit" value="Search" />
    <input type="reset" value="Reset fields" />
  </p>
</form> 



</body>
</html>

<?php
}
?>

 

change to this.

 

you should use else.

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.