2

I have created one HTML form which takes input from user ,Now I need to search user inputed name in Mysql database and print details related to that user inputed name which is stored in Mysql database.
Below script is creating HTML form to take user input, Saved as "ProcessTracking.html".

<form action="details.php" method="get"/>
<h3 align="center"><FONT color=#CCFF66>ENTER SO NUMBER</h3>
<p align="center"> 

<input type="text" id="SO_Number" name="SO_Number"/>

</p>
<div style="text-align:center"> 
<button     type="submit" value="SEARCH">
   <img alt="ok" src=
   "http://www.blueprintcss.org/blueprint/plugins/buttons/icons/tick.png"/> 
   SEARCH
  </button>
 </form>

Below PHP script named as "details.php"

   <?php
$userinput = $_GET['SO_Number'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ProcessTrackingSystem";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}

$result = mysqli_query($conn, "SELECT * FROM   ProcessTrackingSystem.ProcessDetails WHERE SO_Number = '$userinput'") or die(mysqli_error($conn));

$row = mysqli_fetch_assoc($result);
#printf ("SO_Number: %s \n",$row["SO_Number"])
#print_r($row);

printf ("SO_Number:");
printf($row["SO_Number"]);
printf ('--||--');
printf ("Name:");
printf($row["Name"]);
printf ('--||--');

$conn->close();
?>  
Aditya K
  • 161
  • 1
  • 3
  • 13

4 Answers4

1

You have mixed mysql and mysqli api together. Try using either one.

Note: mysql api is deprectaed as of php 5.5.0

chanafdo
  • 5,016
  • 3
  • 29
  • 46
  • 2
    This is not the answer , please try to add this in comment – Dimag Kharab Jun 05 '15 at 05:21
  • You have initialized the connection using `mysqli` and querying the database with `mysql`. They are separate apis. If you need to find a certain row containing the user input you need to add a `WHERE` clause. Don't have the comment privilege yet. :( – chanafdo Jun 05 '15 at 05:27
1

Firstlly you are not using the $_GET['SO_Number'] parameter in a WHERE of SQL statement. Secondlly you are using both mysql and mysqli which are totaly diffrent and don't work together. For usage see mysqli_fetch_row() and mysqli_query(). Also use print_r($row);.

Here is the corrected code:

<?php
$userinput = $_GET['SO_Number'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ProcessTrackingSystem";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
    printf("Connect failed: %s\n", $conn->connect_error);
    exit();
}

$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails WHERE SO_Number = '$userinput'") or die(mysqli_error($conn));

$row = mysqli_fetch_row($result);

print_r($row);

$conn->close();
?>   

EDIT: Added code example.

fsacer
  • 1,382
  • 1
  • 15
  • 23
  • ya i changed sir, now its working , but its showing only results, if i want names of the column than how can i change the script? – Aditya K Jun 05 '15 at 07:01
  • then you print the name like so: printf ("SO_Number: %s (Name: %s)\n",$row["SO_Number"],$row["Name"]); – fsacer Jun 05 '15 at 07:04
  • sir print_r doesn't work for me so i changed to printf, – Aditya K Jun 05 '15 at 07:08
  • Oh yeah I accidently didn't change your previous answer, I just copied it and changed the string. – fsacer Jun 05 '15 at 07:10
0

1st Error

As saty says it is because of the syntax error you have in this line

print_r"$row";

which should be as print_r($row)

2nd Error

You're mixing mysql & mysqli

I am not sure about the table that you have.

Recommendation :

I would recommend you to turn on the error_reporting if not those errors will be in your errors_log file

Also for debugging your sql, you can first construct your sql query, run in the phpmyadmin or related tools for your query, then fire the query and make this done.

Note :

If you are using these code in online then the error_log will be in the directory where you execute this page. (But it may change according to your hosting)

If you are running in local machine the error log may locate according to the server you use...

You can find by printing the php's configuration by phpinfo and find for error_log

Community
  • 1
  • 1
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
0

May this thing will fix your issue

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ProcessTrackingSystem";
    $so = $_POST['SO_Number'];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM ProcessDetails WHERE SO_Number=$so");
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

print_r($row[SO_Number]);
$conn->close();
?>
Sarvagna Mehta
  • 334
  • 3
  • 16