Jump to content

phpMyAdmin MySQL database query


briankopy

Recommended Posts

Thanks in advance for reading/any help.

 

I'm basically trying to create a customer request form (.html page) that will activate a .php file that will pull customers from a MySQL database based on their state that the user enters and on a new page display the customers names and phone numbers.  I'm a noob, There is probably an elegant solution that I can't figure out, I may have to start over etc.

 

The following is the initial html code that allows the user to type in a two letter state abbreviation and click a button.  On that click it will send the state and run "customer_display.php".

 

<html>

<head>

<title>

</title>

</head>

 

<body>

 

<form action="customer_display.php" method="post" name="customer_search">

  <table width="25%" border="0" cellspacing="2" cellpadding="2" align="center">

  <tr>

    <td>Customer Records</td>

    <td> </td>

  </tr>

 

  <tr>

    <td>Single State (optional):</td>

    <td><input type="text" name="state" size="2" maxlength="2" /></td>

  </tr>

 

  <tr>

    <td><input type="submit" value="Submit Query" /></td>

    <td> </td>

  </tr>

 

</table>

</form>

</body>

</html>

 

Here is all I have for customer_display.php It assigns the state data to a varaible called state...

 

<html><head><title></title></head>

 

<body>

 

<?php

 

$state = $_POST['state'];

 

?>

 

</body>

 

</html>

 

I have a MySQL database called "database1" with a table called "customertable".

 

The fields that I want to display on a new page after the user clicks the button are firstname lastname area_code phone_number and I would like to give the user the option to pull these records by state.

 

Again any help would be greatly appreciated.    :)

Link to comment
https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/
Share on other sites

okay I'll give you a kick in the right direction

 

Your need to open a connection to the database, then

query the table, then

display the results

 

here is a basic example (untested)

<?php
$state = $_POST['state']; 

//OPEN CONNECTION
//UPDATE to correct username and password for your database
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
  
if (!mysql_select_db("database1")) {
    echo "Unable to select database1: " . mysql_error();
    exit;
}

//QUERY database

//select every field, in customertable that has the state selected
$sql = "SELECT * FROM customertable WHERE  state='$state' ";

$result = mysql_query($sql);

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;
}


//DISPLAY results
while ($row = mysql_fetch_assoc($result)) {
    echo $row["firstname"];
    echo " "; //space
    echo $row["lastname"];
    //add others
    echo "<BR />";
}

 

I hope that gets you started

Thanks a lot, looks good, but... I received this error

 

Warning: mysql_connect() [function.mysql-connect]:Access denied for user 'user1'@'localhost' (using password: YES) I have my username (user1) and password set up but it is denying me access to the database, any ideas.

 

Thanks.

LOL.. your get used to it..

just takes a little time

$sql = "SELECT * FROM customertable WHERE  state='$state' ";

will find all that have the same state that you posted

 

without the WHERE  state='$state' it will show all

 

PS i am happy to help if you want to learn..

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.