Jump to content

[SOLVED] Anyone know why?


pure_skill_2000

Recommended Posts

Hi

 

Simple piece of php code to search for a record using one veriable input from a form, anyone know why its not working? I have entered all the database connection stuff but not posting it online for security reasons ta muchio

 

<form method="POST" action="<?echo $_SERVER[ "PHP_SELF" ]?>">

Forname: <input type="text" name="query">

<input type="Submit" name="Submit" value="Search">

</form>

 

<?

 

$query = $_POST['query'];

$hostname = ;

$username =  ;

$password = ;

$usertable = "Contacts"; 

$dbName =  ;

$conn = mysql_connect($hostname,$username,$password) or die

('Error connecting to mysql');

//Connect to mysql and verify user

 

 

mysql_select_db($dbname);

//Select appropriate database

if (isset($_POST['Submit'])) {

 

$result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$Array[query]'" )

 

or die("SELECT Error: ".mysql_error());

 

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

 

print $Array[Forname];

}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/
Share on other sites

'$query'  use single quotes.

 

You need to change the last line as well.

 

print $Array[Forname];

 

to

 

print_r ($row);

 

 

Also, the way your logic is, the form will always show up because you are not checking submit until after its displayed.

Is the array empty?

 

Change this

$result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$Array[query]'" )

 

or die("SELECT Error: ".mysql_error());

 

to

 

$result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$query'" )

 

or die("SELECT Error: ".mysql_error());

 

$count = mysql_num_rows($result);

echo $count;

 

 

This is the code at the mo, still not working for me though :(

 

<form method="POST">

Forname: <input type="text" name="query">

<input type="Submit" name="Submit" value="Search">

</form>

 

<?

 

$query = $_POST['query'];

$hostname = ;

$username = ;

$password = ;

$usertable = ;

$dbName = ;

$conn = mysql_connect($hostname,$username,$password) or die

('Error connecting to mysql');

 

 

mysql_select_db($dbName);

if (isset($_POST['Submit'])) {

 

$result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$query'" )

 

or die("SELECT Error: ".mysql_error());

 

$count = mysql_num_rows($result);

echo $count;

 

}

?>

One concern may simply be the fact your using short tags. Also, you need to check the form was actually submitted to avaoid undefined variable warnings.

 

<form method="post">
 Forname: <input type="text" name="query">
 <input type="Submit" name="Submit" value="Search">
</form>

<?php

 if (isset($_POST['Submit'])) {
   $query = mysql_real_escape_string($_POST['query']);
   $hostname = ;
   $username = ; 
   $password = ; 
   $usertable = ; 
   $dbName = ; 
   mysql_connect($hostname,$username,$password) || die('Error connecting to mysql');
   mysql_select_db($dbName);
    $sql = "SELECT * FROM Contacts WHERE Forname='$query'";
   if ($result = mysql_query($sql)) { 
     if (mysql_num_rows($result)) {
       while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
         foreach($row as $k => $v) {
           echo "$k = $v<br />";
         }
       }
     } else {
       echo "No results found";
     }
   } else {
     echo "Query failed<br />" . mysql_error() . "<br />$sql";
   }
 }

?>

 

What does that produce?

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.