**
This is the code for one of several forms saved as php includes for use on the same admin page. They work individually in all browsers but do not work in safari or firefox from the admin page. Any Thoughts?
Thanks for your help!
(I know I need to switch over to prepared statements) **
<?php
$servername = "1.1.1.1:111";
$username = "root";
$password = "root";
$dbname = "sit";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$result = mysqli_query($conn, "SELECT * FROM `calltoaction` ");
$values = mysqli_fetch_array($result);
$calltoaction_title = $_POST['calltoaction_title'];
$calltoaction_content = $_POST['calltoaction_content'];
if(isset($_POST['calltoaction_title'])){
$calltoaction= "UPDATE calltoaction SET calltoaction_title='$calltoaction_title' ,calltoaction_content='$calltoaction_content' WHERE calltoaction_id='1'";
if (mysqli_query($conn, $calltoaction)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
?>
<form id="comment_form" method="post" action="<?php $calltoaction?>" onsubmit="setTimeout(function () { window.location.reload(); }, 10), location.reload(true);">
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="85%">About Us Title</td>
</tr>
<tr>
<td><input class="commentarea" name="calltoaction_title" type="text" id="calltoaction_title"value="<?php echo $values['calltoaction_title']?>"></td>
</tr>
<tr>
<td width="85%" >Testimonial</td>
</tr>
<tr>
<td width="85%" >About Us Content</td>
</tr>
<tr>
<td><pre><textarea class="commentarea" name="calltoaction_content" type="text" id="calltoaction_content" rows= "10" ><?php echo $values['calltoaction_content']?></textarea></pre></td>
</tr>
<tr>
<td>
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
<?php mysqli_close($conn); ?>
Related
I have created php program to order food items. User has been given table. That html table has 4 columns food item,quantity,price,order. User can only give input to order column.
When user give inputs in HTML table and click order button it should be saved in MySQL order table. But I am not able to do that work successfully. I want to do this using only PHP. Without using JavaScript. Help me to solve this.
HTML code
<form action="order2.php" method="POST">
<table border="1">
<tr>
<th>Food Item</th>
<th>Quantity available</th>
<th>Item Price</th>
<th>Order</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result1)) {
?>
<tr>
<td><?php echo $row['fitem']; ?></td>
<td><?php echo $row['qty']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><input type="text" name="order"></td>
</tr>
<?php
}
?>
<tr>
<td>
<input type="submit" value="order" align="right">
</td>
</tr>
</table>
</form>
php code
<?php
session_start();
$order = $_POST['order'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mid";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
while ($row = mysqli_fetch_assoc($result1)) {
$sql = "insert into order (fname,order) values('$row['fitem']',$order)";
if (mysqli_query($conn, $sql)) {
echo "New order created successfully";
} else {
echo mysqli_error($conn);
}
}
}
?>
How can I keep the current form with clear fields after successful insertion. I have used to php files, one for connecting the database and another for collecting data from user. Both of the files code are given below:
For connecting database:
<?php
$servername = "localhost";
$username = "local";
$password = "host";
$dbname = "form";
$conn = mysqli_connect($servername,$username,$password,$dbname);
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
$roll=$_POST['roll'];
$name=$_POST['name'];
$city=$_POST['city'];
$sql = "insert into people_info (roll,name,city) values ('$roll','$name','$city')";
if(mysqli_query($conn,$sql)){
echo "New record created successflly";
}
else{
echo "Error: ".$sql."<br>".mysqli_error($conn);
}
mysqli_close($conn);
?>
And the form page is:
<html>
<body>
<form action="connection.php" method="post">
<table border="1">
<tr>
<th>
Field
</th>
<th>
Value
</th>
</tr>
<tr>
<td>
Roll:
</td>
<td>
<input type="text" name="roll"/><br>
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="name"/><br>
</td>
</tr>
<tr>
<td>
City:
</td>
<td>
<input type="text" name="city"/><br>
</td>
</tr>
<tr>
<td>
<input type="submit"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Instead of this:
if(mysqli_query($conn,$sql)){
echo "New record created successfully";
}
else{
echo "Error: ".$sql."<br>".mysqli_error($conn);
}
try
if(mysqli_query($conn,$sql)){
header('location: page1.php?message=New record created successfully');
}
else{
header('location: page1.php?message=' . mysqli_error($conn));
}
here header() function will redirect you to form page with $message variable having response message. Use it to show the response on page.
This is my table html code. I tried sending the data using the normal insert but it only sends the last row data. I don't know how to send the full data . Can someone please help me with this.
<form action="admin_schedule_employee.php" id="schedule_employee" method="post" >
<input type="date" class="input-sm" name="scheduledate" style="margin:10px;">
<table class="table-responsive table table striped table-bordered">
<thead>
<tr>
<th style="width:20%">Employee First Name</th>
<th style="width:20%">Employee ID</th>
<th style="width:20%">Start Time</th>
<th style="width:20%">End Time</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)): ?>
<tr>
<td><input disabled name="employeename" type="text" value="<?php echo $row['fname']; ?>"></input></td>
<td><input disabled name="employeeid" type="number" value="<?php echo $row['employee_id']; ?>"></input></td>
<td><input name="starttime" type="time"></td>
<td><input name="endtime" type="time"></td>
</tr>
<?php endwhile; ?>
</thead>
<tbody>
</tbody>
</table>
<input type="submit" name="Schedule" value="Schedule">
</form>[This is how my table look like i want to send the whole data to sql database using php][1]
To start with, you will need to create multiple pages:
form.php
process.php
done.php
Creating your user form is simple, place the table in form tags like you have done above, here is an example. Save this page as form.php
<form id="new record" action="process.php" method="POST">
<table width="500px">
<tr>
<td width="50%">
<input type="text" name="fname" id="fname">
</td>
<td width="50%">
<input type="text" name="lname" id="lname">
</td>
</tr>
<tr>
<td width="50%">
</td>
<td width="50%">
<input type="submit" value="Add Record">
</td>
</tr>
</table>
</form>
Next, you will need to create a page which can process this data, and add it to your mysql database. For the following example, I have omitted my database details and substituted them, but you should add your own.
For this example, imagine my database has a table with only an fname and an lname column.
<meta http-equiv="refresh" content="0; url=/done.php" />
<?php
$servername = "your_server_name";
$username = "mysql_username";
$password = 'mysql_password';
$dbname = "database_name";
$fname = $_GET['fname'];
$lname = $_GET['lname'];
try {
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO online (fname, lname)
VALUES ('$fname', '$lname')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record inserted";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
Hopefully, that will work to insert the record. Now we need a table on the done.php page which can display all the records in the database. Use the following code:
<html lang="en">
<head>
<meta http-equiv="refresh" content="5; url=/done.php" />
<meta charset="utf-8" />
<title></title>
</head>
<body>
<?php
$servername = "your_server_name";
$username = "mysql_username";
$password = 'mysql_password';
$dbname = "database_name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * from table_name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["fname"]. ": ";
echo $row["lname"]. "<br /><br />";
}
} else {
echo "No messages";
}
mysqli_close($conn);
?>
</body>
</html>
Hopefully this will work for you.
I've searched and searched I cannot find an answer to this
I've made a form HERE It's very basic. Somehow it's not submitting the final data. the code is
<form class="form" action="submit.php" method="post">
<table>
<tr>
<td>Team Name: </td>
<td><input type="text" name="team"></td>
</tr>
<tr>
<td>Captains xbox tag: </td>
<td><input type="text" name="cap"></td>
<tr>
<td>Captains E-mail:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Team Mates: </td>
<td><TEXTAREA name="teammates" rows="6" cols="50">
Please place each team member on a new line
</TEXTAREA><br></td>
</tr>
<tr>
<td>Sub team mates: </td>
<td><TEXTAREA name="subs" rows="2" cols="50"></TEXTAREA></td>
</tr>
</table>
<p><input type="submit"></p>
</form>
That's the form
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO tournament1 (team, teammates, cap, email, subs)
VALUES ('$_POST[team]','$_POST[cap]','$_POST[email]','$_POST[teammates]','$_POST[subs]')";
if ($conn->query($sql) === TRUE) {
echo "Your team has been submitted Thankyou. You will be redirected back to the tournaments page Shortly";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
What am I doing wrong? It will connect and do something because the ID fills because of its autoincremented. everything else gets lost.
Thanks in advance guys
Kyle
So I switched it around a little for you but this should work fine..
firstly the form, I have added an isset in for you:
<form class="form" action="submit.php" method="post">
<table>
<tr>
<td>Team Name: </td>
<td><input type="text" name="team"></td>
</tr>
<tr>
<td>Captains xbox tag: </td>
<td><input type="text" name="cap"></td>
<tr>
<td>Captains E-mail:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Team Mates: </td>
<td><TEXTAREA name="teammates" rows="6" cols="50">
Please place each team member on a new line
</TEXTAREA><br></td>
</tr>
<tr>
<td>Sub team mates: </td>
<td><TEXTAREA name="subs" rows="2" cols="50"></TEXTAREA></td>
</tr>
</table>
<p><input type="submit" name="sendit"></p>
</form>
You'll notice I have changed action to action="" so it runs under its own page.
Followed next by the PHP of which I have added conditions into for you.
Add the PHP to submit.php
Please note: you can also add required at the end of each input field.
if (isset($_POST['sendit']))
{
$team = $_POST['team'];
$captain = $_POST['cap'];
$email = $_POST['email'];
$mates = $_POST['teammates'];
$sub = $_POST['subs'];
if (!empty($team))
{
if (!empty($captain))
{
if (!empty($email))
{
if (!empty($mates))
{
if (!empty($sub))
{
$sql = "INSERT INTO `tournament1` (team, teammates, cap, email, subs) VALUES ('$team', '$mates', '$captain', '$email', '$sub')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
echo "Please add subs..";
}
} else {
echo "Please add mates..";
}
} else {
echo "Please add captains email..";
}
} else {
echo "Please add captains name..";
}
} else {
echo "Please add team name..";
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have one code for updating information on table on SQL and I am trying to use the same code with minor modifications to make it delete but it is not deleting. I cannot find anything that i am missing here.
<html>
<head>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('my_db6');
$query = "SELECT * FROM emp";
$result = mysql_query($query) or die(mysql_error());
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="1" cellspacing="1" cellpadding="2">
<tr>
<?php
while($row = mysql_fetch_array($result))
{
$ename = $row['ename'];
$empno = $row['empno'];
$position = $row['position'];
?>
<td width="100"></td>
<td><?=$ename?></td>
</tr>
<tr>
<td width="100">E-Name</td>
<td><input name="ename" type="text" value="<?=$ename?>"></td>
</tr>
<tr>
<td width="100">Employee Number</td>
<td><input name="empno" type="text" value="<?=$empno?>"></td>
</tr>
<tr>
<td width="100">Position</td>
<td><input name="position" type="text" value="<?=$position?>"></td>
</tr>
<?php } ?>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['update']))
{
$ename = $_POST['ename'];
$empno = $_POST['empno'];
$position = $_POST['position'];
$sql = mysql_query("UPDATE EMP SET ename = '$ename', empno = '$empno', position = '$position' WHERE ename = '$ename'");
echo "Updated data successfully\n";
}
?>
</body>
</html>
and this is the code of delete i am trying to make :
<html>
<head>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('my_db6');
$query = "SELECT * FROM emp";
$result = mysql_query($query) or die(mysql_error());
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="1" cellspacing="1" cellpadding="2">
<tr>
<?php
while($row = mysql_fetch_array($result))
{
$ename = $row['ename'];
$empno = $row['empno'];
$position = $row['position'];
?>
<td width="100"></td>
<td><?=$ename?></td>
</tr>
<tr>
<td width="100">E-Name</td>
<td><input name="ename" type="text" value="<?=$ename?>"></td>
</tr>
<tr>
<td width="100">Employee Number</td>
<td><input name="empno" type="text" value="<?=$empno?>"></td>
</tr>
<tr>
<td width="100">Position</td>
<td><input name="position" type="text" value="<?=$position?>"></td>
</tr>
<?php } ?>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="delete" type="submit" id="delete" value="delete">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['delete']))
{
$ename = $_POST['ename'];
$empno = $_POST['empno'];
$position = $_POST['position'];
$sql = mysql_query("DELETE EMP SET ename = '', empno = '', position = '' WHERE ename = '$ename'");
echo "Deleted data successfully\n";
}
?>
</body>
</html>
however it doesn't make any changes in the SQL data table entry or deleting
Use this to delete an entry:
mysql_query("DELETE FROM `emp` WHERE `ename`='$ename'");
If you want to edit an entry:
mysql_query("UPDATE `emp` SET `ename`='', `empno`='', `position`='' WHERE `ename`='$ename'");
I recommend you escape $ename before you use it in the query, you can do that like this:
$ename = mysql_real_escape_string($_POST['ename']);
I reckon your delete syntax is wrong. I'm no expert in MySQL, but it doesn't make sense to specify column values in a delete. Try
DELETE FROM emp WHERE ename = '$ename'
And as I now see a couple people have also mentioned in comments, it's very critical that you move to using parameterized queries. This approach is currently vulnerable to very serious SQL Injection attacks.