I'm not able to insert the data with this.
Here is my code:
<?php
include('includes/config.php');
if(isset($_POST['update'])) {
$sampleid=$_POST['sampleid'];
$aba11=$_POST['aba11'];
$aba12=$_POST['aba12'];
$sql="INSERT INTO 'aba1'(sampleid,aba11,aba12) VALUES(:sampleid,:aba11,:aba12)";
$query = $dbh->prepare($sql);
$query->bindParam(':sampleid',$sampleid,PDO::PARAM_STR);
$query->bindParam(':aba11',$aba11,PDO::PARAM_STR);
$query->bindParam(':aba12',$aba12,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
}
?>
<html>
<form>
<body>
<label> Sampleid </label> <input type="text" name="sampleid"><br>
<label> Start time </label> <input type="text" name="aba11"><br>
<label> Stoptime </label> <input type="text" name="aba12"><br>
<button type="submit" name="update" >Update</button>
</Form>
</body>
</html>
My database connection is correct. There is no error in config.php file.
Your form's method was missing and the value in your update button was missing
also, you must use ` instead of ' for database table names
try the code below
<?php
include('includes/config.php');
if(isset($_POST['update'])) {
$sampleid=$_POST['sampleid'];
$aba11=$_POST['aba11'];
$aba12=$_POST['aba12'];
$sql="INSERT INTO `aba1` (sampleid,aba11,aba12) VALUES(:sampleid,:aba11,:aba12)";
$query = $dbh->prepare($sql);
$query->bindParam(':sampleid',$sampleid,PDO::PARAM_STR);
$query->bindParam(':aba11',$aba11,PDO::PARAM_STR);
$query->bindParam(':aba12',$aba12,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
}
?>
<html>
<body>
<form method="POST" action="">
<label> Sampleid </label> <input type="text" name="sampleid"><br>
<label> Start time </label> <input type="text" name="aba11"><br>
<label> Stoptime </label> <input type="text" name="aba12"><br>
<button type="submit" name="update" value="1">Update</button>
</Form>
</body>
</html>
Code seems pretty much ok for me.
Are you certain that $_POST['update'] is set to something?
The lack of error could indicate that it isn't even going through your PHP block.
use form method post
<form method="post" id="insert-data">
</form>
value in insert query put $sampleid,$aba11,$aba12
Related
I'm stumped. I've looked this up on multiple answers on Stackoverflow, but just can't get it. Maybe I'm just not seeing something.
I'm making a Family Feud game and using PHP and MySQL databases to store and retrieve information. For the Fast Money Round, I have a database with a Table called "FastMoney1" I'm using an HTML5 form and PHP to post the data in the form to that table, which has two columns: answer and score
I'm running my query through a for loop, but it's not posting anything to the table. I'm wondering what I'm doing wrong.
HTML:
<form method="post" class="form-horizontal">
<div class="form-group">
<div class="col-xs-9">
<input type="text" class="form-control" id="question1answer" name="answer[0]" placeholder="Question 1">
</div>
<div class="col-xs-3">
<input type="number" class="form-control" id="question1score" name="score[0]" placeholder="0">
</div>
</div>
<div class="form-group">
<div class="col-xs-9">
<input type="text" class="form-control" id="question1answer" name="answer[1]" placeholder="Question 2">
</div>
<div class="col-xs-3">
<input type="number" class="form-control" id="question1score" name="score[1]" placeholder="0">
</div>
</div>
<div class="col-xs-4 col-xs-offset-4" align="center">
<input type="submit" class="btn btn-success" name="Submit" />
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])){
require "config.php";
for ($i = 0; $i<count($_POST); $i++){
$answer = $_POST['answer'][$i];
$score = $_POST['score'][$i];
$sql = "INSERT INTO `fastMoney1`(`answer`, `score`) VALUES ('$answer','$score')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo $conn->error;
}
}
$conn->close();
echo "<meta http-equiv='refresh' content='0'>";
}
?>
All of this lives on the same PHP page, which is why I do not have an action attached to the form.
The config.php is an include that calls the host, username, password and database and opens the connection
Remember that PHP variables as case sensitive you have given name Submit in form while in php you are checking if(isset($_POST['submit'])){ which never become true.
change it to
if(isset($_POST['Submit'])){ //<----- S in upper case
EDIT
You also need to change your loop to
for ($i = 0; $i<count($_POST['answer']); $i++){
see your sql statement you don't need those ampersand in table name and column names INSERT INTO fastMoney1(answer, score) VALUES ('$answer','$score')
<input type="submit" class="btn btn-success" name="submit" />
i changed
name="Submit"
to
name="submit"
"Submit" to "submit" ----->"S" to "s"
and it works fine
I'm using TinyMCE text editor on my website and I have problem with gettingtext from the TinyMCE and then insert to the db. I'm propably blind, but a don't see what wrong. Working with PDO.
Header, activate the editor
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
Forms
<?php include "InsertArticles.php"; ?>
<div id="editor">
<form method="post">
<textarea name="Obsah"></textarea>
</form>
</div>
<div id="inputaddnadpis">
<form method="post">
Nadpis: <input type="text" name="Nadpis">
</form>
</div>
<form method="post">
<input type="submit" name="Article" id="InsertArticles" value="Add article">
<input type="submit" name="Tip" id="InsertTips" value="Add tip">
</form>
Insert
<?php
include_once "db.php";
global $db;
if (!empty($_POST["Article"])) {
$sqlVlozeni = "INSERT INTO WEB_ARTICLE (Nazev, Clanek) VALUES (:nazev, :clanek)";
$sqlProvedeni = $db->prepare($sqlVlozeni);
$stav = $sqlProvedeni->execute(array(":nazev" => $_POST["Nadpis"], ":clanek" => $_POST["Obsah"]));
}
?>
Submitting the third form will not submit values from the first two forms.
In general, only the inputs inside a particular form will be submitted with that form.
Consider using only one <form> element around all of your inputs.
<form method="post">
<div id="editor">
<textarea name="Obsah"></textarea>
</div>
<div id="inputaddnadpis">
Nadpis: <input type="text" name="Nadpis">
</div>
<input type="submit" name="Article" id="InsertArticles" value="Add article">
<input type="submit" name="Tip" id="InsertTips" value="Add tip">
</form>
I am quite new to HTML/PHP code.
I am trying to build a form that will search a MySQL database based on a key value (Vehicle VRN) being provided. As it stands I have sorted the submit code and I am able to add a new customer to the Customers database by clicking 'Submit New'
However, I cannot get the search function to work e.g. enter the vehicle VRN and fill in the rest of the form with that customers information
Here's the HTML form:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ABC Autorite Ltd</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
<script type="text/javascript" src="style/accordian.pack.js"></script>
</head>
<body onload="new Accordian('basic-accordian',5,'header_highlight');">
<div id="logo"><h1>ABC Autorite</h1></div>
<div id="basic-accordian" >
<div id="test-header" class="accordion_headings header_highlight">Customers</div>
<div id="test-content">
<div class="accordion_child">
<h1>Search customer database or submit new details</h1>
<div class="form_layout">
<form method="post" id="Customer">
<select name="title">
<option>Mr.</option>
<option>Dr.</option>
<option>Ms.</option>
<option>Mrs.</option>
</select>
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $first_name; ?>">
<input type="text" name="last_name" placeholder="Last Name">
<input type="text" name="phone_number" placeholder="Phone">
<input type="text" name="email_address" placeholder="Email Address">
<input type="text" name="address_line_1" placeholder="Address">
<input type="text" name="postcode" placeholder="Postcode">
<input type="text" name="vrn" placeholder="VRN">
<input type="text" name="make" placeholder="Make">
<input type="text" name="model" placeholder="Model">
<input type="text" name="year" placeholder="Year">
<div class="form_buttons">
<input type="submit" name="search" Value="Search" onclick="form.action='search.php';"/>
<input type="submit" name="submit" Value="Submit New" onclick="form.action='submit.php';"/>
</div>
</form>
</div>
</div></div>
<div id="test1-header" class="accordion_headings">New Job Card</div>
<div id="test1-content">
<div class="accordion_child">
<h1>Create a new Job Card</h1>
</div>
</div>
<div id="test2-header" class="accordion_headings">Job Cards</div>
<div id="test2-content">
<div class="accordion_child">
<h1>Search for a previous Job Card</h1>
</div>
</div>
</div>
</div>
<div id="footer">
<p>Copyright ABC Autorite Ltd</p>
</div>
</body>
</html>
Here's the PHP search script:
<?php
$servername="192.168.0.8";
$username="my_admin";
$password="my_password";
$dbname="ABCAUTORITE";
// Opens a connection to a MySQL server
$connection=mysql_connect ($servername, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$vrn = $_POST['vrn'];
$sql = mysql_query("SELECT * FROM Customers WHERE vrn like '%$vrn%'");
while($row = mysql_fetch_array($sql))
{
echo $row['first_name'];
echo $row['last_name'];
}
?>
I am just returning first_name and last_name for now as a 'test' before I add the rest of the values.
This has caused me a few hours of head scratching so I am on here looking for any help.
Thanks.
You can separate a form by checking to see what $_POST[] variable isset
<?php # customer.php
if(isset($_POST['search'])){
echo 'pressed the search button.';
while($row = mysql_fetch_assoc($sql)){
$form[] = '<input type="text" name="first_name" value="'.$row['first_name'].'">';
$form[] = '<input type="text" name="last_name" value="'.$row['last_name'].'">';
// etc
}
} elseif(isset($_POST['submit'])){
# execute code to submit user.
} else {
# render the form normally.
$form[] = '<input type="text" name="first_name" placeholder="First Name" value="">';
$form[] = '<input type="text" name="last_name" placeholder="Last Name" value="">';
// etc.
}
?>
And just use a standard form.
<!-- Still in customer.php -->
<form method="post" id="Customer" action="./customer.php">
<?php
foreach($form as $v){
echo $v;
}
?>
<div class="form_buttons">
<input type="submit" name="search" Value="Search" />
<input type="submit" name="submit" Value="Submit New" />
</div>
</form>
And now your question:
To do with PHP and HTML alone, you would need to do everything in 1 page meaning you would need to generate the form with your desired values.
Okay.. you don't have to if you don't want to but I would really recommend it. This way your code is together and not split apart.
Now there's a million ways to code this, I just picked one that you might be able to easily understand.
There is an alternative
A combination of Javascript and PHP. Use Javascript (I would really recommend jQuery) to request a single php file on the server and let it return a json format object. Then use jQuery to update your already rendered form.
On a side note..
Switch over to PDO for your database to allow for binding your post data to your prepared statement. Currently your code is open to SQL injection.
//passing value through url
while($rowcontent=mysqli_fetch_array($details))
{
echo "<tr><td><a href=http://localhost/study/study2/edit.php?toedit=$rowcontent[rollnumber]>edit</a></td><tr>";
}
//receiving value from url
<html>
<form method="GET" action="edit.php">
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
$rollnumber=$_GET["toedit"];
echo $rollnumber;
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}
?>
above are two parts of code, where im trying to edit values in a DB by passing value (a roll number) through url but in editing code the value is not being received correctly or some other problem i cant figure out. i did the same for deleting a value from url but it seems to work.
You can use like below:
//receiving value from url
<html>
<form method="GET" action="edit.php">
<?php
while($rowcontent=mysqli_fetch_array($details))
{
?>
<input type="hidden" name="toedit" value="<?php echo $rowcontent[rollnumber]; ?>" />
<?php
}
?>
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
inside edit.php
<?php
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}
I am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>