php mysql; writing data to database - php

having a bit of trouble adding some data to a database. I have the file new_entry.php which is a form, which posts the data added to insert_new.php.
Every time the fields are filled in and submitted the data does not go to the database with the error message "Could not add the data to table" appearing..any ideas?
NEW_ENTRY.PHP
<body>
<form method="post" action="insert_new.php"><!-- form sent to insert_new.php-->
Section: <input type="text" name="section"/><br />
Food: <input type="text" name="food"/><br />
Description: <input type="text" name="description"/><br />
Price: <input type="text" name="price"/><br />
<br />
<input type="submit" value="submit"/>
</form>
</body>
INSERT_NEW.PHP
<?php
include 'library/connect.php';//connect to databse
$section = $_REQUEST["section"]; // get data from the HTML form on new student form
$food = $_REQUEST["food"];
$description = $_REQUEST["description"];
$price = $_REQUEST["price"];
mysql_query ("INSERT INTO food_menu (section, food, description, price) VALUES ('$section', '$food', '$description', $price)")/* insert the data to the food_menu table*/
or die ("Could not add the data to table");//error message
header('Location:index.php');//auto redirect to view page
include 'library/closedb.php';
?>

It seems that you have a mistake at the end of your MySQL query near price.
Please replace the code below with existing line:
mysql_query ("INSERT INTO food_menu (section, food, description, price) VALUES ('$section', '$food', '$description', '$price')")
Tell me the result please.

First: Don't do this. You really need to research SQL Injection or you will be very sorry.
Secondly, your price has no numeric validation (assuming it's going into a numeric column)... this is also bad... what if someone put in a dollar sign or something?
Next, please post your table definition and connection code (not the connection values).
You can also get more feedback if you do something like:
or die (mysql_error());//error message

Related

Database isn't receiving values from form using php

Im trying to send form data to my sql database but the database isn't receiving any of my values. The name of my database is taxibooking and table name is bookings.
I tried separating the php code in another file and using action on form to access the php code. on clicking submit I was redirected to a blank page with my php file name.
<form method="POST" action="">
Name of customer:<input type="text" name="fname"><br><br>
Enter pickup address:<textarea name="padd" rows="5" cols="10"></textarea><br><br>
Enter destination address:<textarea name="dadd" rows="5" cols="10"></textarea><br><br>
Select Taxi type:<select name="taxi"><option value="Viennese Fiaker">The Viennese Fiaker</option><option value="Indian Auto Rickshaw">Indian Auto Rickshaw</option><option value="Little Yellow">Little Yellow</option><option value="Mumbai Taxi Fiat">Mumbai Taxi Fiat</option><option value="Tricycles">Tricycles</option><option value="Water Taxi">Water Taxi</option><option value="Impeccable Taxi">Impeccable Taxi</option><option value="Red Taxi">Red Taxi</option></select><br><br>
<input type="submit" value="submit" name="sub">
</form>
<?php
$con=mysqli_connect("localhost","root","","taxibooking");
if(isset($_POST['sub']))
{
$n=$_POST['fname'];
$p=$_POST['padd'];
$d=$_POST['dadd'];
$type=$_POST['taxi'];
$sql="insert into bookings(name,pickup,destination,type) values ('$n','$p','$d','$type')";
mysqli_query($con,$sql);
}
?>
Try to debug and put query on if condition
if(mysqli_query($con,$sql)){
echo'done';
}else{
echo "error".$sql."</br>" . mysqli_error($con);
}
Here's how to add parameters on your insert script.
$sql="insert into bookings(name,pickup,destination,type) values ('".$n."','".$p."','".$d."','".$type."')";
I made a new database with a different name with the same table name and properties and now it sort of mysteriously works now. No changes done to the code.

Adding new elements to mysql tables through html

I am trying to add new elements to my mysql table through html
So far I've got this
<form action="MyCurrentFile.php" method="post" >
Artist Name
<input type="text" name="addingnewelement" <br/>
<input type = "submit" name = "Submit" />
</form>
and this
<?php
if (isset($_POST['submit'])) {
$addingelement=$_POST['addingnewelement'];
$mysqli->select_db("names", $names);
echo("this code is running");
$sql='INSERT INTO names (nameValue) VALUES ('.$addingelement.')';
$mysqli->query($sql, $mysqli);
$mysqli->close($mysqli);
}
?>
Is there anything on my syntax wrong? the code does not give me any error, all that happens is that I press my button to update with my input but nothing happens.
you need to put hte artist name as the label and close the input, and make the name of the submit button the same as in your other page.
<form action="MyCurrentFile.php" method="post" >
<label for="artistName">Artist Name</label>
<input type="text" id="artistName" name="addingnewelement" value=""/>
<input type="submit" name="submit" value="submit"/>
</form>
also your sql query is wrong - it should be:
$sql="INSERT INTO names (nameValue) VALUES ('".$addingelement."')";
Your SQL query is wrong. Please replace this live:
$sql="INSERT INTO names (nameValue) VALUES ('$addingelement')";
the submit button name is Submit with capital S but you are using isset($_POST['submit']) with small s
correct this. it will work..
and also if $sql='INSERT INTO names (nameValue) VALUES ('.$addingelement.')'; not working then try
$sql="INSERT INTO names (nameValue) VALUES ('".$addingelement."')";

If input exists from one table update it on another one

Im trying to make like a refill code that will refill my table.
I have one table that have my refill code in it and other table that stores the balance of the account.
table1: card_credit (table that stores the balance of the account)
table2:card_refill (table that have me refill code)
I have created this code with session and PHP. Now I'm stuck and dont know how to move forward.
I want to make when i write in the refill code from table card_refill that its take the amount of credit into value in table card_refill
refill.php
<strong>Refill</strong>
<form action="refill.php" method"post">
<input type="text" name="refillcode"/>
<br>
<input type="submit" name="Submit" value="Refill" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['refillcode'] = $_POST['refillcode'];
}
?>
Here is a possible solution, I am just don't know, where the card_id comes from.
This is inserting a new record into your card_credit table.
// starting the session before any output
session_start();
//include here the database connection file!
//for example:
//include('db_connection.php');
if (isset($_POST['Submit'])) {
//First do a validation here, is the refillcode number, exists, etc...
//Insert it into the table
$sql = "INSERT INTO card_credit (card_id, value) VALUES ('[YOUR_CARD_ID_HERE]', " . intval($_POST['refillcode']) . ")";
//Link is the resource variable when you created the mysqli_connect
mysqli_query($link, $sql);
//Redirect here if you want
}
?>
<!-- HTML CODE STARTS HERE -->
<strong>Refill</strong>
<form action="refill.php" method="post">
<input type="text" name="refillcode"/>
<br>
<input type="submit" name="Submit" value="Refill" />
</form>

PHP Form to Database

Hi I am trying to get a form to post to a database i can connect and the database and table are set up. but rather than post the contents of the fields in it posts the text firstname and secondname in to the columns.
below is my code:
mysql_select_db("company", $conn);
$sqlCmd = sprintf("INSERT INTO names (firstname, secondname) VALUES ('%firstname','%secondname')",
mysql_real_escape_string($_POST["firstname"]),
mysql_real_escape_string($_POST["secondname"]));
//echo $sqlCmd;
//die();
mysql_query($sqlCmd);
mysql_close($conn);
}
?>
<form method="post">
<input type="text" id="firstname" name="firstname"/>
<input type="text" id="secondname" name="secondname"/>
<input name="submit" type="submit" value="Submit"/>
</form>
I need it to post the values from the fields. i am new to php and this is my first project, i would love some help.
just to add this is what i have managed after following a tutorial.
Thanks
Ryan
Do not use php's mysql_ methods any more.
It is outdated:
https://wiki.php.net/rfc/mysql_deprecation
Use mysqli_ or pdo instead
In your code you forgot the mysql_connect() anyways ;)
Just change your sprintf call to:
$sqlCmd = sprintf("INSERT INTO names (firstname, secondname) VALUES ('%s','%s')",
mysql_real_escape_string($_POST["firstname"]),
mysql_real_escape_string($_POST["secondname"]));
Also consider using the newer mysqli function: http://www.php.net/manual/en/intro.mysqli.php
HTH;
Pacific

Using a Submit Button to insert an entry into a MySQL database via PHP?

I'm pretty new to PHP, so I'm not quite sure on what to do with this.
Basically I'm trying to insert an entry into my MySQL database, through a "submit" button in HTML. I can't seem to get this to work, is it possible?
<?php
include('db_connect.php');
$SQL = "INSERT INTO chosenitems (ID, Name, Price) VALUES ('', '4-6 Days', '£75.00')";
$result = mysql_query($SQL);
?>
The INSERT works perfectly fine on its own, but I want it to be executed when the "submit" button is pressed.
Any help would be greatly appreciated.
Thanks
Tobo.
Just set the action of the form to the URL of the script that performs the insert.
Note that since you are modifying a database, the request is probably non-idempotent and you should use the POST method.
<form action="/path/to/your/script.php" method="post">
<input type="submit">
</form>
<form method="post">
<input type="submit" name="submit" value="submt"/>
</form>
PHP
<?php
if(isset($_POST['submit']))
{
$SQL = "INSERT INTO chosenitems (ID, Name, Price) VALUES ('', '4-6 Days', '£75.00')";
$result = mysql_query($SQL);
}
?>
You can check button value is posted and can execute line of code in it.
<?php
include('db_connect.php');
if(isset($_REQUEST['SUBMIT_BUTTON_NAME']))
{
$SQL = "INSERT INTO chosenitems (ID, Name, Price) VALUES ('', '4-6 Days', '£75.00')";
$result = mysql_query($SQL);
}
?>
Hope this will be helpful to you
I had for the submit details:
<form id = "submitForm" action="config/profile_save.php" method="post">
<button type="submit" class="button" name="submit" value="submit">Save Profile</button></form>
Inside each input field on the page, I placed form = "submitForm"
I then changed the name too.(This is the super global variable later)
<input type="text" autofocus="true" class="custom_link_url_text" id="custom_link_url_text"
name="custom_link_email" placeholder="Enter your public email address" spellcheck="false"
style="width: 245px;" maxlength="75" form = "submitForm">
I was then able to capture the data on the next page using the name as POST variable.
if(isset($_POST['submit'])) {
$custom_link_email = $_POST['custom_link_email'];
}
Once I did that it was just a case of inserting data into the database.

Categories