Forum Moderators: coopster
<body>
<?php
mysql_connect("localhost", "root", "bigboldy") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("test") or die(mysql_error());
echo "Connected to Database";
?>
<form name="form1" method="POST" action="find_name.php">
<select name="name">
<option value="jamie">Jamie</option>
<option value="helen">Helen</option>
<option value="pauline">Pauline</option>
<option value="david">David</option>
</select>
<input name="Submit" type="submit" value="submit"/>
</form>
</body>
<body>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
&name = $_POST['name'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get data from mysql
$sql = "SELECT 'name', 'lastname', 'email' FROM $tbl_name WHERE 'name'=$name";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "$row['name']".
echo "$row['lastname']".
echo "$row['email']";
}
else {
echo "ERROR";
}
// Close connection
mysql_close();
?>
</body>
echo $sql; statement doesn't show me anything in the browser.
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
&name = $_POST['name'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get data from mysql
$sql = "SELECT name, lastname, email FROM $tbl_name WHERE name='$name'";
echo $sql;
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "$row['name']".
echo "$row['lastname']".
echo "$row['email']";
}
else {
echo "ERROR";
}
// Close connection
mysql_close();
?>
<body>
<body>
<?php
//Turn error reporting on, this will help loads when your developing!
error_reporting(E_ALL);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
//Assign the vars from the previous page
$name = strip_tags(mysql_real_escape_string($_POST['name']));//make this safer for the DB
// Connect to server and select database.
$conn = mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");
// Get data from mysql
//Use the correct quoting quotes, Single quotes for string values & not column names
//Back ticks for the column names & not values (this is optional, you will find out later on! ;))
//Int's dont need quoting, but strings do when used in the sql
$sql = "SELECT `name`, `lastname`, `email` FROM `".$tbl_name."` WHERE `name` = '".$name."';
$query = mysql_query($sql, $conn) or die("cannot connect");
//Once the connection handle has been established, you don't need to re reference it because the functions
//always use the last known connection that is still available, hope that makes sense
while ($row = mysql_fetch_array($query)) {
echo $row['name']."<br>";
echo $row['lastname']."<br>";
echo $row['email']."<br>";
}
//cant have an else on a while; else's belong on an if...
// Close connection
// no need for this function as the connection automatically closes after the last query has finished ;)
mysql_close();
?>
</body>
<?php
mysql_connect("localhost", "root", "bigboldy") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("test") or die(mysql_error());
echo "Connected to Database";
?>
<form action="find_name.php" method="post" name="form1">
<select name="name">
<option value="Jamie">Jamie</option>
<option value="Helen">Helen</option>
<option value="Pauline">Pauline</option>
<option value="David">David</option>
</select>
<input name="Submit" type="submit" value="submit"/>
</form>
<?php
//Turn error reporting on, this will help loads when your developing!
error_reporting(E_ALL);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
//Assign the vars from the previous page
$name = strip_tags(mysql_real_escape_string($_POST['name']));//make this safer for the DB
// Connect to server and select database.
$conn = mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");
// Get data from mysql
// Use the correct quoting quotes, Single quotes for string values & not column names
// Back ticks for the column names & not values (this is optional, you will find out later on! ;))
// Int's dont need quoting, but strings do when used in the sql
$sql = "SELECT `name`, `lastname`, `email` FROM `".$tbl_name."` WHERE `name` = '".$name."'";
$query = mysql_query($sql, $conn) or die("cannot connect");
//Once the connection handle has been established, you don't need to re-reference it because the functions
//always use the last known connection that is still available, hope that makes sense
while ($row = mysql_fetch_array($query)) {
echo $row['name']."<br>";
echo $row['lastname']."<br>";
echo $row['email']."<br>";
}
//cant have an else on a while; else's belong on an if...
// Close connection
// no need for this function as the connection automatically closes after the last query has finished ;)
mysql_close();
?>
<form action="find_name.php" method="post" name="form1">
<select name="name">
<option value="Jamie">Jamie</option>
<option value="Helen">Helen</option>
<option value="Pauline">Pauline</option>
<option value="David">David</option>
</select>
<input name="Submit" type="submit" value="submit"/>
</form>
<?php
//Catch the form being processed
if(isset($_POST['submit']) && ($_POST['submit'] == "submit")){
//Turn error reporting on, this will help loads when your developing!
error_reporting(E_ALL);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
//Assign the vars from the previous page
$name = strip_tags(mysql_real_escape_string($_POST['name']));//make this safer for the DB
// Connect to server and select database.
$conn = mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");
//added the echo so that you can see if things are set correctly (if they are you will see a name in the vars place ;))
echo $sql = "SELECT `name`, `lastname`, `email` FROM `".$tbl_name."` WHERE `name` = '".$name."'";
$query = mysql_query($sql, $conn) or die("cannot connect");
while ($row = mysql_fetch_array($query)) {
echo $row['name']."<br>";
echo $row['lastname']."<br>";
echo $row['email']."<br>";
}
}
else{
//if these files are in the same directory, and 'name' is the form this will redirect you back there
header("location: name.php");
}
?>
<?php
//Turn error reporting on, this will help loads when your developing!
error_reporting(E_ALL);
//Catch the form being processed
if(isset($_POST['Submit']) && ($_POST['Submit'] == "submit")){
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
$conn = mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");
//Assign the vars from the previous page
//still assign the security functions to the $_POST value
//Be aware though, that 'mysql_real_escape_string()' requires a DB connection to work
//but because there is already a connection going at this point in the script, the function
//will automatically pick it up without you needing to reference it in the function.
//if it doesn't pick it up just change the comments over
//they should both work though!
//$name = strip_tags(mysql_real_escape_string($_POST['name'], $conn));
$name = strip_tags(mysql_real_escape_string($_POST['name']));
//do the Sql query - you should see a value there now too ;)
echo $sql = "SELECT `name`, `lastname`, `email` FROM `".$tbl_name."` WHERE `name` = '".$name."'";
$query = mysql_query($sql, $conn) or die("cannot connect");
//see if the query returned anything ie > 'more than' 0 rows
if(mysql_num_rows($query) > 0){
//loop through the results
while ($row = mysql_fetch_array($query)) {
echo $row['name']."<br>";
echo $row['lastname']."<br>";
echo $row['email']."<br>";
}
//close while loop
}else{
//set up the handler just in case the query returns no results!
echo "The query returned no results";
//optional, but if there is nothing else to do with the script you can kill it
exit;
}
//close the if
}
else{
//set the 'error handler' part of the script up
//if these files are in the same directory, and 'name' is the form this will redirect you back there
header("location: name.php");
}
//close the else
?>