getting data from a textarea into a database - php

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.";}
}
}
?>

Related

Not able to insert data using html form and php using wamp [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 1 year ago.
I am trying to insert data into a database from HTML form using php. I made two files html form and other is PHP script. When I click on submit in html form, it shows me the php code. I am using wamp server for database. I put my html files in C:/wamp64/www directory and html files at my local directory. The database table is :
id int(11)
fname varchar(30)
salary int(11) . Id is not auto-incremented and it is a primary key.
Html code:
<html>
<body>
<h2>Employee's Information</h2>
<form action="employee.php" method="POST">
<label for="id">Enter employee id:</label><br>
<input type="text" id="id" name="id" value=""><br>
<label for="fname">Enter First name:</label><br>
<input type="text" id="fname" name="fname" value=""><br><br>
<label for="salary">Enter Employee Salary:</label><br>
<input type="text" id="salary" name="salary" value=""><br><br>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
Php code:
<?php
$mysql_hostname="localhost";
$mysql_username="root";
$mysql_password="";
$mysql_database="employee";
$con=mysql_connect($mysql_hostname,$mysql_username,$mysql_password);
if(!$con){
die('Connection Error: '.mysql_error());
}
mysql_select_db($mysql_database, $con);
if(isset($_POST['submit']))
{
$s_id = $_POST['id'];
$s_name = $_POST['fname'];
$salary = $_POST['salary'];
$employeeinsert = "INSERT INTO employee1
(id, fname, salary)
VALUES('".$s_id."','".$s_name."','".$salary."')";
if(!mysql_query($employeeinsert,$con)) {
echo "Error: " .mysql_error($con);
} else {
echo "1 record added";
}
}
?>
The code is neither giving any error on submitting data nor it is inserting the data into the database.
I am not getting what the error is.
If this is false then the code successfully produces no output:
if(isset($_POST['submit']))
Which is what's happening, since the condition is false. The form has a submit button, but that button has no name attribute to its value isn't sent to the server:
<input type="submit" value="Submit">
Give it a name:
<input type="submit" name="submit" value="Submit">
It's always a good idea to have some kind of indication of any given code branch, even if just logging something somewhere so you can see what's happening. Code will happily produce no output/result if that's what it's instructed to do, but as you've discovered it can leave you with no information about what's happened.
As an aside, and this is important, your code is wide open to SQL injection. You'll want to start addressing that.

Multiple html file to one php file? "closed"

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.

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."')";

how to update mySQL database using a post method in php

i have created a leaderboard for a website which displays users high scores for a game. but whe the user goes to edit their high score, it doesnt change in the database or on the screen. does anybody know how to update the database using a post method. my code is below.
require_once('../sokodatabase.php');
//require_once('../sokodatabase.php');
//require_once('../sokodatabase.php');
if(isset($_POST['userId'])){
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$query = "
UPDATE leaderboardhighscores
SET highScores=".$_POST["highScores"].", rankNo=".$_POST["rankNo"]."
WHERE userId=".$_POST["userId"];
var_dump($_POST);
echo $query;
#mysqli_query($dbc, $query);
}
}
$manager = new DatabaseManager;
$manager->SelectHighScores();
?>
<form method="post" action="highScores.php">
high score <input type="text" name="highScores"/>
rankNo <input type="text" name="rankNo"/>
userId <input type="text" name="userId"/>
<input type="submit" value="Submit">
</form>
You have to provide attention to SQL injections!
Normally, you check for the submit button:
<input type="submit" name="submit" value="Submit">
Then
if(isset($_POST['userId'])){
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
goes to:
if(isset($_POST['submit'])){
If your query does not work, you can make die($query) to see it and perform it via phpMyAdmin. Or you can use mysqli_error to display any occured error after executing it.
Please note, that with your code only numeric values are possible. If your fields are not numeric, you should use this:
$query = "
UPDATE leaderboardhighscores
SET highScores='".mysqli_real_escape_string($dbc, $_POST["highScores"])."', rankNo='".mysqli_real_escape_string($dbc, $_POST["rankNo"])."'
WHERE userId=".intval($_POST["userId"]);
Need name for the submit input type in-order to submit the form...
Like
<input type="submit" name="userId" value="Submit">
if(isset($_POST['userId']))

PHP form post to MySQL error

I'm creating a php-post form, containing: Who, What, Where, Contact and date_created.
I've made a database with these rows.
Here's my HTML Form code:
<form id="contactform" action="post.php">
<p class="contact"><label for="who">Who</label></p>
<input id="who" name="who" placeholder="Who are you? (First & Second name)" required="" tabindex="1" type="text">
<p class="contact"><label for="email">What</label></p>
<input id="what" name="what" placeholder="What do you want?" required="" type="text">
<p class="contact"><label for="username">Where</label></p>
<input id="where" name="where" placeholder="Country, City, Street..." required="" tabindex="2" type="text">
<p class="contact"><label for="password">Contact</label></p>
<input type="text" id="contact" name="contact" placeholder="Phone number or email"required="">
<br><br>
<input class="buttom" name="submit" id="submit" tabindex="5" value="Submit" type="submit">
And here's the php post.php code:
<?php
// Grab our POSTed form values
// Note that whatever is enclosed by $_POST[""] matches the form input elements
$who = $_POST["who"];
$what = $_POST["what"];
$where = $_POST["where"];
$contact = $_POST["contact"];
// Connect to our DB with mysql_connect(<server>, <username>, <password>)
$sql_connection = mysql_connect("server_name", "admin", "password");
mysql_select_db("database_name", $sql_connection);
$sql = "INSERT INTO content (
who,
what,
where,
contact,
date_created
)
VALUES (
'$who',
'$what',
'$where',
'$contact',
NOW()
)";
mysql_query($sql, $sql_connection);
mysql_close($sql_connection);
?>
When I try to post something, nothing is happening. The screen is just white, the database is empty and the url is like this:
http://my-website.com/post.php?who=Firstname+Secondname&what=Some+sentences+here-and&where=America&contact=some#website.com&submit=Submit%21
Just as HamZa DzCyberDeV said, you didn't specify which method you're using in <form> tag.
For situations when you're POSTing something in your database, just as you are now - use method="post" and for forms when you're searching for something, use method="get".
In case of using post method, your URL will change to only my-website.com/post.php and in case of using get method, your URL will change to something like my-website.com/post.php?... (where your things which you're getting are going) - just how you got URL after submitting.
The screen is just white because post.php (where you're going after clicking on submit button) doesn't contain anything to send to output, which you can easily do with echo.
For instance, you can make a new html page which will be written down with echo:
echo '
<html
<body>
This is my website!
</body>
</html>
';
Also, what you could do is to use include() php script which has already formed HTML, or you can check out here for some other redirect methods:
http://php.about.com/od/learnphp/ht/phpredirection.htm
Just remember that PHP is language which server is processing and only HTML tags (with CSS and JS) are sent to other browser to be read.
For more about POST and GET method you can read here:
http://php.net/manual/en/reserved.variables.post.php
http://php.net/manual/en/reserved.variables.get.php
why don't you try this to get an error or a clue to what is going wrong, enclose your code in try and catch blocks:
try {
// your code
} catch ( Exception $e ) {
echo $e->getMessage();
}

Categories