mysql cell in textbox - php

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

Related

Undefined index: id in C:\xampp\htdocs\test\edit.php on line 13

The given code is a demonstration of crud operation in PHP.
the basic HTML form has three field NAME, EMAIl, DEPT, and a submit button.
the basic operations are working fine add delete and form submission but when I want to update the data it gave
Undefined index: id in C:\xampp\htdocs\test\edit.php on line 13.
but when I changed the query in my edit.php filed id to name it will work fine but it is not working when a change the name to id in my query in edit.php file
the table name is crud. and id is the primary key in the table. the id is auto incremented.
I don't understand why it is working with name and not to work with id filed.
<html>
<head>
<title>CRUD</title>
</head>
<style type="text/css">
form{
text-align: center;
margin-top:50px;
font-family:serif;
color: grey;
}
input{
margin:10px;
}
</style>
<body>
<form action="submit.php" method="POST">
ID:<input type="text" name="id" style="width:200px"; disabled required placeholder="Autogenrated">
<br>
Name:<input type="text" name="name" style="width:200px";required><br>
email:<input type="email" name="email" style="width:200px";required><br>
Dept:<input type="text" name="dept" style="width:200px"; required><br>
<button type="submit" name="submit" style="width:80px";>Submit</button>
</form>
</div>
</body>
</html>
<?php
include_once('db.php');
if (isset($_GET['edit'])){
$id=$_GET['edit'];
$sql=$conn->query("select * from crud where id=$id");
while($row=$sql->fetch_array()){
$name=$row['name'];
$email=$row['email'];
$dept=$row['dept'];
}
}
if (isset($_POST['update'])){
$id=$_POST['id'];
echo $id ;
}
?>
<form action="" method="POST">
ID:<input type="text" name="id" value="<?php echo $id ?>" style="width:200px"; disabled required placeholder="Autogenrated" >
<br>
Name:<input type="text" name="name" value="<?php echo $name ?>" style="width:200px";required><br>
email:<input type="email" name="email" value="<?php echo $email ?>" style="width:200px";required><br>
Dept:<input type="text" name="dept" value="<?php echo $dept ?>" style="width:200px"; required><br>
<button type="submit" name="update" style="width:80px";>Update</button>
</form>
<?php
include_once('db.php');
if (isset($_GET['del'])) {
$id=$_GET['del'];
$sql="delete from crud where id='$id'";
mysqli_query($conn,$sql);
}
header('Location:display.php');
?>
<?php
include_once('db.php');
$sql="select * from crud";
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
while ($row=mysqli_fetch_assoc($result)) {
echo "--Name:".$row["name"]."--Email:".$row["email"]." "."--dept:".$row["dept"]."";
echo "<br>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "<br>";
echo '<td>add</td>';echo "<br>";
}
}
?>
<?php
$hostname="localhost";
$user="root";
$pass="";
$dbname="test";
$conn=mysqli_connect($hostname,$user,$pass,$dbname);
?>
change this
if (isset($_POST['update'])){
$id=$_POST['id'];
echo $id ;
}
to
if (isset($_POST['update'],$_POST['id'])){
$id=$_POST['id'];
echo $id ;
}

Insert checkbox corresponding quantity into database

I have multiple checkbox in my form and the person need to input the quantity of the types of item that is selected. Now, my problem is that I can't get the data to be inserted into database.
This is my add_record.php code:
<?php
include("connect.php");
include("header.php");
$sql_student = "SELECT * FROM student";
$result_student = mysql_query($sql_student);
?>
<form method="post" id="add_form" action="add_record.php">
<label>Name</label>
<input placeholder="Enter Student Name" type="text" name="name" id="name" class="form-control" />
<br />
<input placeholder="Enter Student ID" type="text" name="stud_id" id="stud_id" class="form-control" />
<br />
<?php
$sql_baggage = "SELECT * FROM baggage";
$result_baggage = mysql_query($sql_baggage);
?>
<label>Bag Types</label></br>
<table style="border:none;">
<?php while($row_bag = mysql_fetch_array($result_baggage))
{
$baggage_id = $row_bag['baggage_id'];
?>
<tr>
<td><?php echo $row_bag['baggage_id'];?>
<td><?php echo $row_bag['baggage_type'];?></td>
<td><input type="checkbox" name="tick[]" value="<?php echo $baggage_id;?>"/></td>
<td><input type="text" size="2" name="txt[<?php echo $baggage_id;?>]" placeholder=" "></td>
<?php
?></td></tr>
</table>
<br />
<input type="submit" name="submit" id="submit" value="Add Record" class="btn btn-success btn-secondary pull-right" />
</form>
<?php
if(isset($_POST['submit']))
{
$name = $_POST["name"];
$stud_id = $_POST["stud_id"];
$stu_query = "INSERT INTO student(student_id,student_name) VALUES ('$stud_id','$name')";
if(mysql_query($stu_query))
{
if(!empty($_POST['tick']))
{
foreach($_POST['tick'] as $selected)
{
$qty = $_POST['txt'][$selected];
$inv_query = "INSERT INTO inventory (invstu_id,invbag_id,invbag_quantity) VALUES
('$stud_id','$selected', '$qty')";
if(mysql_query($inv_query))
{
echo'<script>alert("A record has been inserted!")</script>';
}
else
{
echo "Database error";
}
}
}
else
{
echo'<script>alert("A record has been inserted!")</script>';
}
}
}
?>
</body>
</html>
I know that the data is passed through foreach function since I get the echo of database error two times when I tick two of the checkbox. However, the value is not inserted into the database.
Finally solve the issue by echoing the mysql_error(), there is nothing wrong with the code. Just a bit problem at the database. Thanks!!

Form inserting a blank row into the data table

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.

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

addressbook in php

hallo!
I am a university student, and I want to make an address book, using php, where someone can view a table with all the listed contacts' details (e.g. name, phone, email) and there will be the possibility of
-adding a new contact and
-editing
-deleting an existing contact.
I have created a table with the relevant fields in my database, and I have written some code (found on the internet but it didn't work + 20hours of trying to make it work on my own.... without the proper result) which shows the table of contacts, but:
The edit doesn't work at all
When the delete works, it deletes the contacts from last to first and not the row I am selecting
when I put the code for deleting in comments, the "Add" works
when the code for deleting is functional, then the "Add" actually "Deletes" (unless the table is empty, in which case it adds up to ONE contact).
If anyone can give me some hints/suggestions on what to change, please please do!
Here is my code:
<html>
<head>
<title>Address Book</title>
</head>
<body>
<?php
mysql_connect("localhost", "mydb", "mypassword") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
if (isset($_POST['mode']))
{
$mode = $_POST['mode'];
$id = $_POST['id'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email= $_POST['email'];
if ($mode=="add")
{Print '<h2>Add Contact</h2> <p>
<form action="" method=post>
<table>
<tr><td>Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td colspan="2" align="center">
<input type="hidden" name="mode" value="added" />
<input type="submit" value="Submit" />
</td></tr>
</table> </form> <p>'; }
if ($mode =="added")
{mysql_query ("INSERT INTO address (name, phone, email) VALUES ( '$name', '$phone', '$email')");
echo "New contact added successfully!";}
if ($mode=="edit")
{Print '<h2>Edit Contact</h2> <p>
<form action="" method=post>
<table>
<tr><td>Name:</td><td><input type="text" value="';
Print $name; print '" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" value="';
Print $phone; print '" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" value="';
Print $email; print '" name="email" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" /></td></tr>
<input type=hidden name=mode value=edited>
<input type=hidden name=id value='; Print $id; print '>
</table>
</form> <p>';
}
if($mode=="edited")
{mysql_query ("UPDATE address SET name = '$name', phone = '$phone', email = '$email' WHERE id = $id");
Print "Data Updated!<p>";}
if ($mode=="remove")
{mysql_query ("DELETE FROM address where id=$id");
Print "Entry has been removed <p>";}
}
$data = mysql_query("SELECT * FROM address ORDER BY name ASC")
or die(mysql_error());
Print '<h2>Address Book</h2><p>
<form action="" method=post>
<table border cellpadding=3>
<tr><th width=100>Name</th><th width=100>Phone</th><th width=200>Email</th><th width=100 colspan=2>Admin</th></tr>
<td colspan=5 align=right>
<input type ="hidden" name = "mode" value="add"/> <input type = "submit" value="Add Contact"/>';
while($info = mysql_fetch_array( $data ))
{
Print "<tr><td>".$info['name'] . "</td> ";
Print "<td>".$info['phone'] . "</td> ";
Print "<td> " .$info['email'] . "</td>";
Print '<td>
<input type ="hidden" name="mode" vlaue="edit"/>
<input type ="submit" value="Edit"
?id='. $info['id'] .'&name=' . $info['name'] .'
&phone=' . $info ['phone'] .'&email=' . $info['email'] .'/></td>';
Print "<td>
<input type ='hidden' name='mode' value='remove'/>
<input type ='hidden' name='id' value = ".$info['id']." />
<input type ='submit' value = 'remove' /> </td></tr> ";
}
Print "</table>";
Print " </form>";
if(!$mode) echo "You may add, edit or delete a contact";
echo $mode;
?>
</body>
</html>
The reason why things aren't behaving the way you expect is that all your rows are being put into one big form. Therefore there are multiple hidden fields <input type ='hidden' name='id' value = ".$info['id']." />, all of which get submitted when you click any of your 'submit' buttons. But actually the value that will be passed along to your script will be the last value i.e. the last row id.
One way you could get around it is to use a link with the ID as a $_GET argument in the URL, for example:
Edit
Then you can change the line near the top to use $mode = $_GET['mode']; and $id=$_GET['id']
Once you have got the ID in the way, you can display a form with editable fields just for that particular id.
if ($mode == 'edit')
{
$data = mysql_query("SELECT * FROM address WHERE id=$id");
// then fetch the row data and populate the HTML form
}
Of course, the above example is vulnerable to SQL injection http://php.net/manual/en/security.database.sql-injection.php so a slightly more robust method would be wise - see the link for advice!

Categories