Jump to content

PHP Noob Needs Some Help


JM2010

Recommended Posts

Hey All,

 

I have tried and tried with no luck creating a PHP Search forum.

 

What I want to do, is to allow User X, to type in his ID Number and for the site to search a database that would return a number that is associated with his ID Number.  How do I do this.  I have the database created, I think, so how do I go about creating a web page that wold allow this.  Thanks, any help is useful!

Link to comment
https://forums.phpfreaks.com/topic/165067-php-noob-needs-some-help/
Share on other sites

I have tried and tried with no luck creating a PHP Search forum.

 

You mean form, right?

 

HTML Forms - You need to use forms to allow the user to input and submit data.

PHP MySQL - Upon submission you should grab the id with the POST method and query the database where it matches the id and return the fields you want.

like this?

 

<?php

$var = $_POST['var'];

 

$result = mysql_query("SELECT * FROM table WHERE id='$var'");

if($result == 1) { // check if row exist

    $result=mysql_fetch_array($result); //get row content

        $id = $result['associatedNumber']; //get the value of the field

}

echo $id;

 

?>

 

<html>

<head><title>hello</title></head>

<body>

<form method="post" action="index.php">

      <input type="text" name="var">

      <input type="submit" value="submt" name="submit">

</form>

<body>

</html>

 

like this?

 

<?php

$var = $_POST['var'];

 

$result = mysql_query("SELECT * FROM table WHERE id='$var'");

if($result == 1) { // check if row exist

    $result=mysql_fetch_array($result); //get row content

        $id = $result['associatedNumber']; //get the value of the field

}

echo $id;

 

?>

 

<html>

<head><title>hello</title></head>

<body>

<form method="post" action="index.php">

      <input type="text" name="var">

      <input type="submit" value="submt" name="submit">

</form>

<body>

</html>

 

 

There's a couple things wrong with this.

 

First, never ever take user input and use it directly in a query.

 

<?php
$result = mysql_query("SELECT * FROM table WHERE id = '". mysql_real_escape_string($var) ."'");
?>

 

Second, mysql_query won't tell you how many rows are returned. It will either return false because of an invalid query, or a resource handler (for certain query types). So even if it validates to true, that doesn't mean there's any rows found, just that the query successfully ran.

 

<?php
if(mysql_num_rows($result) > 0) { // check if row exist
?>

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.