Form inserting a blank row into the data table - php

I am quite new to this and this problem has been on my back for a few days now.
I have searched for answers to my problem but couldn't find anything satisfying for what I need.
I am trying to send some data through a form into a database and then represent the respective data into a table that shows specific rows.
My problem is that one of those rows (the Actions row) appears stored as blank data eventhough the user fills the form with something...anything.
This is my Add.php file:
<?php
$name="";$first="";$last="";$email="";$pass="";$acti="";
mysql_connect("localhost","admin") or die (mysqli_error());
$db=mysql_select_db("test") or die (mysql_error());
$con=mysqli_connect("localhost","admin","","test")or die (mysqli_error());
if (isset($_POST['save']))
{
$name=mysql_real_escape_string($_POST['name']);
$first=mysql_real_escape_string($_POST['first']);
$last=mysql_real_escape_string($_POST['last']);
$email=mysql_real_escape_string($_POST['email']);
$pass=mysql_real_escape_string($_POST['pass']);
if(isset($_POST['acti']){$actions=mysql_real_escape_string($_POST['acti']);}
echo "A new record has been added!";
$query="INSERT INTO users (Name,FirstName,LastName,Email,Password,Actions)
VALUES ('$name','$first','$last','$email','$pass','$acti')";
$result=mysql_query($query)
or die (mysql_error());
}
mysqli_close($con);
?>
//the form is included in the file
<html>
<head>
<body>
<br/><br/><h2 align=center ><b> Registration page </b></h2><br/><br/><br/>
<form enctype="multipart/form-data" method="POST" action="" align=center >
Name: <input type="text" name="name" value="<?php echo $name;?>"> <br/>
First Name:<input name="first" input type="text" value="<?php echo $first;?>"> <br/>
Last Name:<input name="last" input type="text" value="<?php echo $last;?>"> <br/>
Email: <input type="text" name="email" value="<?php echo $email;?>"> <br/>
Password:<input type="password" input name="pass" value="<?php echo $pass;?>"> <br/>
Actions:<input name="action" input type="text" value="<?php echo $acti;?>"><br/<br/>
<input type="submit" name="save" value="Save">
</form>
<p align=center>Click here for a list!</p>
</body>
</head>
</html>
And this is my List.php file:
<?php
mysql_connect("localhost","admin") or die (mysql_error());
mysql_select_db("test") or die (mysql_error());
/*mysql_query("CREATE TABLE users(id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),Name VARCHAR(255),FirstName VARCHAR(255),LastName VARCHAR(255),Email VARCHAR(255),Password VARCHAR(255),Actions VARCHAR(255))") or die (mysql_error());*/
echo"<table border='2' align=center>";
echo"<tr> <th>Name</th> <th>Email</th> <th>Actions</th> </tr>";
$result=mysql_query("SELECT * FROM users")or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr> <td>";
echo $row['Name'];
echo"</td> <td>";
echo $row['Email'];
echo"</td> <td>";
echo $row['Actions'];
echo"</td> </tr>";
}
echo"</table>";
?>
Thank you in advance!

You're calling $_POST['acti']; when the field name is name='action'.
You've put:
if(isset($_POST['acti']){$actions=mysql_real_escape_string($_POST['acti']);}
^ acti ^acti
Your HTML:
Actions:<input name="action" input type="text" value="<?php echo $acti;?>"><br/<br/>
^action, not 'acti'
Either use name='acti' in your form, or $_POST['action'] in your PHP.
Please look at this link on how to use mysqli properly.

you are mixing between mysql and mysqli
switch all mysql codes to mysqli example:
$name=mysql_real_escape_string($_POST['name']);
should be
$name=mysqli_real_escape_string($_POST['name']);
this
$result=mysql_query($query)
or die (mysql_error());
should be
$result=mysqli_query($query)
or die (mysqli_error());
this
if(isset($_POST['acti'])
should be
if(isset($_POST['action'])
BTW: you connecting by two methodes ?? mysql and mysqli ??
remove this:
mysql_connect("localhost","admin") or die (mysqli_error());//this connection is mysql and is also wrong without password.
$db=mysql_select_db("test") or die (mysql_error());
and connect only by mysqli.

Related

How can i update the records which is already in a database in php

I want to update the radio button value, but it shows me message "something went wrong"(i.e. my query doesn't get execute). on submit click it went to a new page (i.e update.php)......please help me!
it also shows an error of undefined index 'id'??
//main.php
<?php
//connecting to the database
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="company";
#mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to mysql");
#mysql_select_db("$db_name") or die("no database");
$query = "SELECT driver_name,vehicle_no FROM driver_info"; //You don't need a ; like you do in SQL
$html="";
$rs=mysql_query($query);
while($row=mysql_fetch_array($rs))
{
$html.='
<form action="update.php" method="get">
<tr>
<td>'.$row['driver_name'].'</td>
<td>'.$row['vehicle_no'].'</td>
<td><label class="checkbox">
<input type="radio" value="free" name="stat" required="required"></label></td>
<td><label class="checkbox">
<input type="radio" value="travel" name="stat" required="required"></label></td>
<td><input type="submit" value="update" name="submit"></td>
</tr>
</form>';
}
?>
//update.php
<?php
include('data_conn.php');
$id=$_GET['id'];
$stat=$_GET['stat'];
$query ="UPDATE driver_info SET status=$stat WHERE id=$id";
$result = mysql_query($query);
if(!$result) {
echo '<script language="javascript">';
echo 'alert("something went Wrong...:(((("); location.href="search.php"';
echo '</script>';
}
else{
echo '<script language="javascript">';
echo 'alert("successfully updated!!!"); location.href="search.php"';
echo '</script>';
}
?>
I think problem occurs because you are not passing id in GET.
Try this:
//main.php
<?php
//connecting to the database
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="company";
#mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to mysql");
#mysql_select_db("$db_name") or die("no database");
$query = "SELECT id,driver_name,vehicle_no FROM driver_info"; //You don't need a ; like you do in SQL
$html="";
$rs=mysql_query($query);
while($row=mysql_fetch_array($rs))
{
$html.='
<form action="update.php" method="get">
<input type="text" name="id" value='.$row['id'].' style="display:none"><br>
<tr>
<td>'.$row['driver_name'].'</td>
<td>'.$row['vehicle_no'].'</td>
<td><label class="checkbox">
<input type="radio" value="free" name="stat" required="required"></label></td>
<td><label class="checkbox">
<input type="radio" value="travel" name="stat" required="required"></label></td>
<td><input type="submit" value="update" name="submit"></td>
</tr>
</form>';
}
?>
//update.php
<?php
include('data_conn.php');
$id=$_GET['id'];
$stat=$_GET['stat'];
$query ="UPDATE driver_info SET status=".$stat." WHERE id=".$id";
$result = mysql_query($query);
if(!$result) {
echo '<script language="javascript">';
echo 'alert("something went Wrong...:(((("); location.href="search.php"';
echo '</script>';
}
else{
echo '<script language="javascript">';
echo 'alert("successfully updated!!!"); location.href="search.php"';
echo '</script>';
}
?>
Where do you get $_GET['id']? To me it seems that its empty so the update query wont execute because it has no ID.
$id=$_GET['id'];
this is empty and has no value assigned so the query won't work.
Try this code:
$query ="UPDATE driver_info SET status=".$stat." WHERE id=".$id";

Need to edit data using combobox's data in php

i am fetching few data to edit this. in edit page all the data is showing from database. but i want to add combobox with data on that edit page.
i have below code for database connecting.
<?php
$event_id=0;
if(isset($_REQUEST['event_id']) && $_REQUEST['event_id']>0)
{
$event_id = $_REQUEST['event_id'];
}
if($event_id>0)
{
$username="root";
$password="1amShaw0n";
$database="shawon_logindb";
$dbhost = 'localhost:3306';
mysql_connect($dbhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM shawon_logindb.event_table WHERE event_id = '$event_id'";
$result = mysql_query($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
mysql_close();
}
?>
also in edit page i am showing the data to edit using below code.
<form name="edit_form" method="post" action="event_save_edited_data.php" />
<input type='hidden' name='event_id' value="<?php echo $_REQUEST['event_id']; ?>" />
<table>
<tr>
<th width="194" bgcolor="#999999" scope="row"><div align="center" class="style13">
<div align="center">Event Type:</div>
</div></th>
<td width="500"><input type="text" name="event_type" value="<?php echo isset($_POST['event_type'])?$_POST['event_type']:isset($row[1])?$row[1]:''; ?>" ></td>
</tr>
But i want to edit this using combobox's data. Please let me know the required changes.
Another way is to echo the whole statement, using the correct type as text is not a combobox and use empty instead of isset.
<?php
if(empty($_POST['event_type']))
echo "<select name='event_type' value='$_POST['event_type']'>";
else if(empty($row[1]))
echo "<select name='event_type' value='$row[1]'>";
else
echo "<select name='event_type' value=''>";
?>
Then your options to edit and close it at the bottom:
<option value="type_1">type_1</option>
<option value="type_2">type_2</option>
<option value="type_3">type_3</option>

mysql cell in textbox

I want to have a cell in my mysql database as the value in a textbox.
I want to have the FirstName column for person with CustomerID of say, 1 in the textbox.
This is my coding.
<html>
<head>
<title>Edit Customer</title>
</head>
<body>
<?php
mysql_connect("localhost","cmcclintock","000547") or die(mysql_error());
mysql_select_db("fleet hire motors") or die(mysql_error());
$CustomerID = $_GET["customerid"];
$query=mysql_query("SELECT FirstName FROM customer WHERE CustomerID = $CustomerID") or die(mysql_error());
$row = mysql_fetch_array($query);
echo ($CustomerID);
echo ($query);
echo ($row);
?>
<form name="custedit2" method=GET action="editcustomersubmit.php">
First Name: <input name="FirstName" type="text" value="<?php echo ($query); ?>">
<input name="submitbtn" type="submit" value="Save">
<input name="resubmitbtn" type="submit" value="Reset">
</form>
</body>
</html>
Hopefully this shows enough to explain what I am looking for.
<input name="FirstName" type="text" value="<?php echo ($row['FirstName']); ?>">
$row is where your result set is stored, so you use this to display the column that you want

Editing data from MySQL via PHP

I am running into a frustrating problem with a PHP script that's supposed to allow me to edit individual rows within my MySQL database.
This is the file where all of the rows from the database are displayed; it works just like it's supposed to.
<table cellpadding="10">
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>E-mail</td>
<td>Phone</td>
</tr>
<?php
$username="username here";
$password="password here";
$database="database name here";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);
mysql_close();
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[id]</td>");
echo ("<td>$row[first]</td>");
echo ("<td>$row[last]</td>");
echo ("<td>$row[email]</td>");
echo ("<td>$row[phone]</td>");
echo ("<td>Edit</td></tr>");
}
echo "</table>";
?>
As you can see, each row has an "Edit" link that is supposed to allow the user to edit that individual student's data. Here, then, is StudentEdit.php:
<?php
$username="username";
$password="password";
$database="database";
mysql_connect(localhost,$username,$password);
$student_id = $_GET[id];
$query = "SELECT * FROM students WHERE id = '$student_id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
mysql_close();
?>
<form method="post" action="EditStudentData.php" />
<table>
<tr>
<td><input type="hidden" name="id" value="<? echo "$row[id]" ?>"></td>
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="first" value="<? echo "$row[first]" ?>"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="last" value="<? echo "$row[last]" ?>"></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phone" value="<? echo "$row[phone]" ?>"></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" value="<?echo "$row[email]" ?>"></td>
</tr>
</table>
</form>
When I execute this, however, I get the following error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home4/lukaspl1/public_html/StudentEdit.php on line 12
Any ideas what's wrong, and how to fix it?
Thank you in advance!
Remove the mysql_close from here
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);
mysql_close();
The code should mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);
And moreover,you are going to use only key based resultset.. simply have mysql_fetch_assoc.
And another suggestion would be instead of $row[id]..replace it with $row['id'].
StudentEdit.php: you forgot to call #mysql_select_db($database) or die( "Unable to select database"); before you executed the query
This part of the code is wrong:
$student_id = $_GET[id];
the correct code is
$student_id = $_GET['id'];
code from expertsnote.com
Try...
echo ("<td>Edit</td></tr>");
instead of
echo ("<td>Edit</td></tr>");
this code was missing
$select_db = mysql_select_db("$db_name");
if (!$select_db) {echo "Error Selecting Database";}
this is the cod for edit the details dynamically
<?php
include('db.php');
$id=$_REQUEST['id'];
$query="SELECT * FROM `camera details` WHERE id='".$id."'";
$result=mysqli_query($db,$query) or die(mysqli_error());
$row1=mysqli_fetch_assoc($result);
if(isset($_POST['submit'])&&(isset($_POST['new'])&&($_POST['new'])==1))
{
$id=$_REQUEST['id'];
foreach($_POST as $key=>$values)
{
if($key!="submit"){
$names[]=$key;
$val[]= "'".$values."'";
if($key!="new"){
$k[] = "`".$key."` = '".$values."'";
}
}
}
$output=implode(",",(array)($k));
//$v=implode(",",(array)($val));
// `name` = 'san'
$query="UPDATE `camera details` SET $output WHERE id='".$id."'";
$output=mysqli_query($db,$query) or die(mysqli_error($db));
if($output)
{
header('location:cameralist.php');
}
}
else{
?>
I recommend doing this in studentEdit.php
$student_id = mysql_real_escape_string($_GET[id]);
$query = "SELECT * FROM students WHERE id = '$student_id'";
$result = mysql_query($query) or die(mysql_error() . ' ' . $query);
$row = mysql_fetch_array($result);
mysql_close();
Two things I've changed here is firstly to escape the data being passed in the url and secondly I've added or die(mysql_error() . ' ' . $query); If something is going wrong in the sql statement you should now see the error and hopefully you'll be able to fix it from there.
What looks incorrect to me is the way you are displaying the value retrieved from the database:
<input type="hidden" name="id" value="<? echo "$row[id]" ?>">
It should be
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
This code gives the option to add, search, edit and delete options. Thought it might to see all the options in one code.
$searchedUsername = "";
$searchedEmail = "";
//registration (Add) function
if ( isset($_POST['stdregister'])){
$username = $_POST['stdusername'];
$password = $_POST['stdpassword'];
$email = $_POST['stdemail'];
$hashedPassword = md5($password);
$connection = mysqli_connect("localhost","root","","std");
$query = "INSERT INTO student VALUES ('$username','$hashedPassword','$email')";
if ( mysqli_query($connection,$query) == 1 ){
echo "Successfully saved";
}
else{
echo "<p style='color: #f00;'>There is an error</p>";
}
mysqli_close($connection);
}
//delete function
if ( isset($_POST['stddelete'])){
$username = $_POST['stddelusername'];
$connection = mysqli_connect("localhost","root","","std");
$query = "DELETE FROM student WHERE username LIKE '$username'";
mysqli_query($connection,$query);
echo mysqli_error($connection);
mysqli_close($connection);
}
//update function
if ( isset($_POST['stdupdate'])){
$username = $_POST['stdusername'];
$stdpass = md5($_POST['stdpassword']);
$stdemail = $_POST['stdemail'];
$connection = mysqli_connect("localhost","root","","std");
$query = "UPDATE student SET password='$stdpass', email='$stdemail' WHERE username LIKE '$username'";
mysqli_query($connection,$query);
echo mysqli_error($connection);
mysqli_close($connection);
}
if ( isset($_POST['stdsearch']) ){
$searchUsername = $_POST['stdeditusername'];
$connection = mysqli_connect("localhost","root","","std");
$query = "SELECT * FROM student WHERE username LIKE '$searchUsername' ";
$result = mysqli_query($connection, $query);
while( $row = mysqli_fetch_array($result) ){
$searchedUsername = $row['username'];
$searchedEmail = $row['email'];
}
}
?>
<html>
<head>
</head>
<body>
<h1>Student Registration</h1>
<form name="stdregistration" action="forms.php" method="post">
<label>Username :</label>
<input name="stdusername" required="required" type="text" /><br /><br />
<label>Password :</label>
<input name="stdpassword" type="password" /><br /><br />
<label>E-mail :</label>
<input name="stdemail" type="email" /><br /><br />
<input name="stdregister" type="submit" value="Save" />
</form>
<h2>Delete Students</h2>
<form name="stddeletion" action="forms.php" method="post">
<label>Select the Username :</label>
<select name="stddelusername" required>
<option value="">Select One</option>
<?php
$connection2 = mysqli_connect("localhost","root","","std");
$query2 = "SELECT username FROM student";
$result = mysqli_query($connection2,$query2);
while( $row = mysqli_fetch_array($result) ){
echo "<option value='".$row['username']."'>".$row['username']."</option>";
}
mysqli_close($connection2);
?>
</select>
<input name="stddelete" type="submit" value="Delete" />
</form>
<h2>Edit Students</h2>
<form name="stdedition" action="forms.php" method="post">
<label>Select the Username :</label>
<select name="stdeditusername" required>
<option value="">Select One</option>
<?php
$connection2 = mysqli_connect("localhost","root","","std");
$query2 = "SELECT username FROM student";
$result = mysqli_query($connection2,$query2);
while( $row = mysqli_fetch_array($result) ){
echo "<option value='".$row['username']."'>".$row['username']."</option>";
}
mysqli_close($connection2);
?>
</select>
<input name="stdsearch" type="submit" value="Search" />
</form>
<form name="stdedit" action="forms.php" method="post">
<label>Username :</label>
<input name="stdusername" required="required" type="text" readonly value="<?php echo $searchedUsername; ?>" /><br /><br />
<label>Password :</label>
<input name="stdpassword" type="password" /><br /><br />
<label>E-mail :</label>
<input name="stdemail" type="email" value="<?php echo $searchedEmail; ?>" /><br /><br />
<input name="stdupdate" type="submit" value="Update" />
</form>
</body>
</html>

Database form does display results

I am having trouble displaying my results in the form. Could anyone show me what I am doing wrong? The only thing that is showing up is the echo Database Output. I am trying to create a database to update a webpage. I am suppose to go to the admin page which contains this form and should be able to add, delete and update the webpage any suggestions would help. Thanks in advance.
</head>
<body>
<?php
$id=$_POST['id'];
$db="";
$link = mysql_connect('localhost', '', '');
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$query=" SELECT * FROM tblContent WHERE PageID ='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$pageHeading=mysql_result($result,$i,"PageHeading");
$subHeading=mysql_result($result,$i,"SubHeading");
$contentTxt=mysql_result($result,$i,"Content");
$pageTitle=mysql_result($result,$i,"PageTitle");
$metaDescription=mysql_result($result,$i,"MetaDescription");
$metaKeywords=mysql_result($result,$i,"MetaKeywords");
?>
<form method="post" action="admin.php">
<input type="hidden" name="ud_id value="<? echo $id; ?>">
LinkText: <input type="text" name="ud_LinkText" value="<? echo $contectTxt; ?>"><br>
Page Heading:<input type="text" name="ud_PageHeading" value="<? echo $id; ?>">
Sub Heading:<input type="text" name="ud_SubHeading"
value="<? echo $subHeading; ?>"><br>
Page Title: <input type="text" name="ud_PageTitle" value="<? echo $pageTitle; ?>"><br>
MetaDescription: <input type="text" name="ud_MetaDescription"
value="<? echo $metaDescription; ?>"><br>
MetaKeywords: <input type="text" name="ud_MetaKeywords"
value="<? echo $metaKeywords; ?>"><br>
<input type="Submit" value="Update">
</form>
<?php
++$i;
}
?>
</body>
</html>
$db=""; //add your database name here
You probably get an error message due to this mis spelled function:
$num=mysql_numrows($result);
should be
$num=mysql_num_rows($result);

Categories