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
https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/
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() );
?>

thank you for helping. should it be like this?

 

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

  //I got rid of the bottom line and changed $sql to $result.

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>

 


<?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.

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.