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

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

 

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;

 

}

?>

Link to comment
Share on other sites

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?

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.