I want to print the name and last name of an ID entered in the text box. Here is the PHP and HTML code:
<head>
<title>
Search your name by ID
</title>
</head>
<?php
if(isset($_POST["searchname"]))
{
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
$a = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($a);
echo "$row[0] , $row[1] , $row[2]";
}
else
{
echo "error";
}
?>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
Output when I enter ID:
, ,
There are entries in the MySQL table but I am unable to fetch them. What is wrong with my code?
UPDATE: I have also tried mysql_fetch_array but it is not working.
Main problem is that you're miximg mysqli and mysql. These are absolutely different APIs.
Assuming you have
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
Next you should:
$result = $connect->query("Select * from users where id='$id'");
Then get results:
while ($row = $result->fetch_assoc()) {
var_dump($row);
}
And of course, instead of directly putting values into your query use prepared statements.
Update:
about mistakes:
Your main mistake is mixing apis. When you use mysql (which is deprecated and you mustn't use it anymore) you can't use any of mysqli functions and vice versa.
Next - as you create mysqli object with new, you should work in object-oriented style, i.e. calling methods from your mysqli object.
Try this:
<html>
<head>
<title>
Search your name by ID
</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST["searchname"])){
$id = $_POST["searchname"];
$connect = mysql_connect("localhost","adarsh","Yeah!","adarsh");
$result = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($result);
print_R($row);
}else{
echo "there is something wrong";
}
Related
I've changed the code following people advices but my delete button doesn't work. The empID is a VARCHAR, not an INT
The way i wanted it to be done when i search a string of letters i would get a list of employees containing that string, then choose some checkboxes and when button is pressed they'd get deleted from the DB and the list of not chosen would still stay on that page.
Thanks in advance for any help!!!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Delete Record</title>
<link rel="stylesheet" href="style1.css" />
<style>dialog{margin-left:100px}
select { font-size:24px;}</style>
</head>
<body>
<div class="header">
<h2>List of the employees with the name entered</h2>
</div>
<form name="action_form" action="" method="post" />
<div class="input-group">
<input type="text" name="name" placeholder="Employee name" />
</div>
<button type="submit" class="btn" name="submit">SEARCH</button>
<?php
require('db.php');
$errors = array();
if(isset($_POST["name"])&&!empty($_POST["name"]))
{
$name=$_POST['name'];
$sqlResult=mysqli_query($con, "SELECT * FROM Employee WHERE empName LIKE '%$name%'");
if (mysqli_num_rows($sqlResult) > 0)
{
echo "<table>";
while($row=mysqli_fetch_assoc($sqlResult))
{
echo "<tr>";
echo "<td>"; ?><input type= 'checkbox' name='num[]' value='<?php echo $row['empID'] ?>'/><?php echo "</td>";
echo "<td>".$row['empID']."</td>";
echo "<td>".$row['empName']."</td>";
echo "<td>".$row['deptNo']."</td>";
echo "<td>".$row['addCounty']."</td>";
echo "<td>".$row['salary']."</td>";
echo "</tr>";
}
echo "</table>";
}
if(isset($_POST['delete'])&&(!empty($_POST['num'])))
{
$list = array();
$list = $_REQUEST['num'];
foreach($list as $delID)
{
$sqlResult = mysqli_query($con,"DELETE FROM employee WHERE empID LIKE '$delID'");
}
}
}
?>
<div class="input-group">
<label>Please choose the person from the list below</label>
</div>
<div class="input-group">
<button type="submit" class="btn" name="delete">FIRE SELECTED</button><br><br>
<button type="reset" class="btn" name="reset">RESET</button><br><br>
Back to the Menu
</div>
</form>
</body>
</html>
Try this :
if(isset($_POST["name"])&&!empty($_POST["name"]))
{
$name=$_POST['name'];
$sqlResult=mysqli_query($con, "SELECT * FROM Employee WHERE empName
LIKE '%$name%'")
}
The reason for the error (Undefined variable $name) is because you are only setting $name in your "if" statement when $_POST['name'] is set, but you are running the line:
$sqlResult = mysqli_query($con, "SELECT * FROM Employee WHERE empName LIKE '%$name%'");
every time the page is loaded. Because you have used $name in the SQL string, but it isn't always declared, you get the error.
I'm finding your code a little hard to read, but I think you probably just want to put the mysqli_query() line inside the "if" statement.
if(isset($_POST['name'] && !empty($_POST['name'])) {
$name = $_POST['name'];
$sqlResult = mysqli_query($con, "SELECT * FROM Employee WHERE empName LIKE '%$name%'");
}
Looks like you should wrap the $delId in "%"
So your delete query should look like this:
$sqlResult = mysqli_query($con,"DELETE FROM employee WHERE empID LIKE '%$delID%'")
Also bear in mind that the like statement will delete any row where the id is like any other id. You might consider changing this to:
$sqlResult = mysqli_query($con,"DELETE FROM employee WHERE empID = '$delID' ")
Another thing to keep in mind is that you should consider using parameterized queries to prevent sql injection. Read thise for more details:
What is parameterized query?
Hello im trying to delete which ever value is selected in a drop down list.
I cant seem to understand what is going on
I have 2 pages 1 with my connection and functions to view the table in a drop down (which works) and a delete function (which doesn't seem to work) and another to call the function in and to delete which ever value is selected.
connection.php
<?php
//Connect to the database
function getSQLConnection() {
$mysqlConnection = new PDO('mysql:host=localhost;dbname=isad235_100000', "root", "");
return $mysqlConnection;
}
//Get all results from members table
function getResults($tablename) {
$sql = "SELECT * FROM " . $tablename;
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
//Delete results from members table
function deleteValue($id) {
$sql = "DELETE FROM members WHERE member_id = '$id'";
$mysqlConnection = getSQLConnection();
$ResultSetting = $mysqlConnection->query($sql);
return $ResultSetting;
}
?>
delete.php
<?php
include_once 'connection.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add</title>
</head>
<body>
<h1> Delete a Member from the Members Table. </h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
Delete Member:
<select name='members' value='members'id="Mmembers">
<?php
$results = getResults('members');
if ($results) {
foreach ($results as $row) {
echo '<option value="' . $row['member_id'] . '">' . $row['name'] . '</option>';
}
}
else
echo '<option value="0"0"> No Data</option>';
?>
</select>
<input type="submit" id="delete" value="Delete"/>
<br/>
<br/>
</form>
<?php
if (isset($_POST['members'])) {
$ResultSetting = deleteValue(($_POST['members']));
}
?>
<br/>
<br/>
<form action='index.php' method='GET'>
Go Back:
<input type="submit" name="submit" value="Return"/>
</form>
<br/>
</body>
</html>
I ran your code and don't see any errors with it. Make sure the id column on your 'members' table is called 'member_id'. If there is a discrepancy in the name then the values for the option elements wouldn't be set. Also, the value you just deleted would still appear after the initial page submit. If you reload the page after the submit, you'll see the value has disappeared.
I'm new to PHP and MySQL. I am trying to make a simple search form using which I would want to show the results from the database based on the input text entered in the form.
My code is like this:
Form.php
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<title>test</title>
</head>
<body>
<form action="search.php" method="GET" id="form">
Name: <input type="text" name="name" >
Age:<input type="text" name="age">
Search<input type="submit" name="submit" id="Search" Value="Search">
</form>
</body>
</html>
Connect.php
<?php
$connect = mysql_connect('localhost','$user','$password');
if(!$connect){
die('Could not connect'.mysql_error() );
}
$db_selected = mysql_select_db('test');
if(!$db_selected){
die('wrong'.mysql_error() );
}
?>
Search.php
<?php
include("includes/connect.php");
$name=$_GET['name'];
echo $name;
$query = "SELECT * FROM `cats` WHERE name='\$name'";
$results= mysql_query($query);
if (!empty($results)){
echo "query successful" ;
exit;
}
$row=mysql_fetch_assoc($results);
echo "Age:".$row['age'];
echo "Name:".$row['name'];
?>
The echo $names ouputs the result correctly and so does echo "query successful".
However the
echo "Age:".$row['age'];
echo "Name:".$row['name'];
only echo's the string part and the query does not seem to fetch any results.
I tried changing the mysql_fetch_assoc to mysql_fetch_array, but it does not do anything either. Could anyone tell me what am i doing wrong here. My DB table has two columns and two rows.
You're escaping the $ in the variable by doing \$.
Try:
$query = "SELECT * FROM `cats` WHERE name='$name'";
EDIT
From the discussion below.
The problem with the undefined index is the fact that you are using $row['age'] when really, the column name in the database is Age. Therefore you must use $row['Age'] when referring to the item. The same goes for name.
$query = "SELECT * FROM `cats` WHERE name='" . $name . "'";
or without concatenation
$query = "SELECT * FROM `cats` WHERE name='$name'";
This should work:
$query = "SELECT * FROM cats WHERE name = '$name'";
Also, when you call "exit;" in the following block it will cancel the execution of the rest of your script:
if (!empty($results)){
echo "query successful" ;
exit;
}
I have a simple dropdown menu which posts the result to itself but when i choose one of the options in the drop down menu it does not echo back the result as expected.
I'm sure i've just missed out something simple but can't spot it. Any ideas? The form posts but does not echo back $user_settings.
<?php
include "functions.php";
connect();
$sql="SELECT user_id, user_realname FROM users ORDER BY user_realname ASC";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$name=$row['user_realname'];
$options.="<OPTION VALUE=>".$name.'</option>';
}
if(isset($_POST['submit'])){
$user_realname = $_POST['username_select'];
$user_select = mysql_query("SELECT user_id, user_realname FROM users WHERE user_realname = '$user_realname'")
or die ("Could not get user data");
while($row = mysql_fetch_array($user_select)){
$user_settings = $row['user_id'];
echo $user_settings;
}
}
?>
<html>
<head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="POST">
<tr><label>Choose User to Edit</tr>
<tr><SELECT NAME="username_select"><OPTION VALUE=""></option>User's Name<?php echo $options;?></SELECT></label></tr>
<tr><input type="submit" value="submit" name="submit"></tr>
</form>
<?php echo $user_settings;?>
<br/>
Go Back
</body>
</head>
</html>
User $_SERVER['PHP_SELF'] instead of $PHP_SELF
Please help, Im trying to search for mysql records using an html form to display the corresponding record for the entered primary key.
Here's my html form:
<td><input type="submit" name="Submit" value="Search"></td>
And here's the new.php form action:,
mysql_select_db("Hospital", $con);
$result = mysql_query("SELECT HOSPNUM FROM t2 WHERE FIRSTNAME='{$_POST["fname"]}'");
while($row = mysql_fetch_array($result))
{
<input name="hnum" type="text" id="hospnum" value="<?php echo $row['HOSPNUM']; ?>" />
}
mysql_close($con);
?>
How do I get to display the hospnum in the html inputbox when I input the fname and then click the search button.
Note: This script, as-is, is vulnerable to sql-injections. The code that follows is not dealing with this, as it's out of the scope of the original question. Do not use this code as-is in a production environment.
You have a small problem jumping from PHP to HTML:
<?php
mysql_select_db("Hospital", $con) or die(mysql_error());
$fname = $_POST["fname"];
$result = mysql_query("SELECT HOSPNUM FROM t2 WHERE FIRSTNAME='{$fname}'");
?>
<h3>Results:</h3>
<?php while ( $row = mysql_fetch_array($result) ) { ?>
<input type="text" name="hnum" value="<?php echo $row["HOSPNUM"]; ?>" />
<?php } ?>