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."')";
Related
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.
i am trying to get text from a text box into my database, but it wont go through. i have tried so many things please help!! the else statement always executes, because I get the message "no submission received on my webpage", which means the first if statement definitely executes.
As FirstOne said you need to name the input "submit".
<input class="input" type="submit" name="submit" value="شارك"/>
Hello There are two problem's with your code ..
First one add name attr in your submit button because you are checking isset($_POST['submit'])
<input class="input" type="submit" name="submit" value="شارك"/>
Second Update Your $query with this
$query= "INSERT INTO hamsasubmissions (secret,popularity) VALUES ('".$_POST["newSecret"]."',0)";
first of all you didn't give the submit button a name so you must name it 'submit' to match what you wrote in your code and also your SQL query seems to be incorrect, here's a snippet with the desired changes:
<form method="post" action="post.php">
<textarea name="newSecret" id="help" class="textarea" rows="20" cols="100">
</textarea>
<input class="input" name="submit" type="submit" value="شارك"/>
</form>
<?php
if(isset($_POST['submit'])) {
// trim possible begining/ending whitespaces from the the textarea value. But you still need to escape it againt SQL injection !
$newSecret = trim($_POST['newSecret']);
if(isset($newSecret)[0]) {
include "db_connect.php";
$query= "INSERT INTO hamsasubmissions (secret,popularity) VALUES ('" . $newSecret . "', 0)";
if(!mysqli_query($mysqli,$query)){
echo "no submission received";}
else{echo "Secret submitted.";}
}
}
?>
I am sorry if has any unclear sentences beacuse I am bad in explaining thingy..
So i have three sample file name.html, time.html and insert.php.. in name.html, after i click the button and it will pop up another window (time.html). Then in time.html after i click the submit button, the insert.php will execute.
name.html
<form method = "post">
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
time.html
<form method="post" action="insert.php">
<input type="time" name="time_name" id="my_time">
<br><br>
<input type="submit" name="myButton" id="myButton">
</form>
insert.php
<?php
$name = $_POST['student_name'];
$time = $_POST['time_name'];
if(mysqli_query($connect, "INSERT INTO student (student_name, time)
VALUES ('$name', '$time')")){
....}else{ ...}
My question: Is there a way that the insert.php can retrieve both name and time information in the html files but not only in time.html.
If I add in the insert.php file in name.html, the php file will execute first.
<form method = "post" action= "insert.php">
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
::Most of the sentence makes no sense, if any unclear sentence please let me know.. Also, i couldn't think any better title for this question..
Edit: Seems like i found the solution.. I just add an action in the name.php.
In name.html i change to name.php
<form method = "post" action = "<? include 'time.php' ?>" >
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
In time.html change to time.php
Thanks for helping me out!!
Your question: Is there a way that the insert.php can retrieve both name
and time information in the html files but not only in time.html.
If I add in the insert.php file in name.html, the php file will
execute first.
The way you explained is a bit confusing ! but anyway if you want include the php to your name.html !
1. Either instruct Apache to treat .html files as PHP(See This) or make it as a .php file
and if you want to restrict your php code from executing first or automatically ! update your code with
if(isset($_POST['something'])){ //all other stuff here }
On your code
<?php
if(isset($_POST['submit'])) { //executes only when the submit button clicked !
$name = $_POST['student_name'];
$time = $_POST['time_name'];
if(mysqli_query($connect, "INSERT INTO student (student_name, time)
VALUES ('$name', '$time')")){
....}else{ ...}
You can also make some more conditions to check whether $name & $time values are entered or not also simply you can validate it with making it as required in html itself
Also in a better way you can do the same with AJAX and some JS See the answer here
You have two files with two forms. And you have 1 php file to process both. And you want the data from both files in your insert.
Then why don't you create only 1 file, 1 form, ask for both info, insert once.
So your form would become:
<form action="index.php" method="POST">
<h3>Name: </h3><input type="text" name="student_name" id="studentName">
<h3>Time: </h3><input type="time" name="time_name" id="my_time">
<input type="button" name="submit">
</form>
Obviously this above code is to illustrate, I do not take care of everything. Then you have to check SQL injection, validate your inputs, ...
As far as I know, there is no way to get data from 2 forms into a single action, since every time you click submit, it executes the action.
Unless your first form defines it's action as the second form. Then the second form creates a to pass the result of the first form along with it's input to the action file that does the insert to database.
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.
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