Why is my data not being submitted into my database? - php

I need to connect an html form to my sql database and I'm starting out with a simple form so I can understand how it works. I can't figure out what I am doing wrong. I have the form created, the php script, and the database created. Whenever I submit the form it takes me to a blank page and nothing has been added to my database. I've written error messages if the connections fail but I'm not seeing those either. I'm not as advanced in programming can someone please help me?
index1.html
<html>
<body>
<form action="info.php" method="post">
<p>Username:<input type="text" name="username" /></p>
<p>Email:<input type="text" name="email" /></p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
info.php
<?php
define('DB_NAME' , 'users' );
define('DB_USER' , 'root');
define('DB_PASSWORD' , '');
define('DB_HOST' , 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
//connection to host
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//error if not connected to host
$db_selected = mysql_select_db(DB_NAME, $link);
//select the database
if(!$db_selected){
die('Can\'t use ' . DB_NAME . ':' . mysql_error());
}
echo 'Connected successfully';
$value = $_POST['username'];
$value2 = $_POST['email'];
$sql = "INSERT INTO guests (username, email) VALUES ('$value', '$value2')";
if(!mysql_query($sql)){
die('Error: ' . mysql_error());
}
//error check to see if connected to tables
mysql_close();
//close connection
?>

As I stated in comments; your mysql_ code checks out, so chances are you need to use either mysqli_ or PDO, since mysql_ functions may very well not be available for you to use.
If the following mysqli_ rewrite does not work for you, then the problem goes deeper and would be out of scope of the question.
Using error reporting will/should confirm that. Consult "Footnotes" on how to use it in your PHP file(s).
<?php
define('DB_NAME' , 'users' );
define('DB_USER' , 'root');
define('DB_PASSWORD' , '');
define('DB_HOST' , 'localhost');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
//connection to host
if (!$link) {
die('Could not connect: ' . mysqli_error($link));
}
echo 'Connected successfully';
$value = mysqli_real_escape_string($link,$_POST['username']);
$value2 = mysqli_real_escape_string($link,$_POST['email']);
$sql = "INSERT INTO guests (username, email) VALUES ('$value', '$value2')";
if(!mysqli_query($link, $sql)){
die('Error: ' . mysqli_error($link));
}
//error check to see if connected to tables
else{
echo "Data entered";
}
mysqli_close($link);
//close connection
?>
References:
MySQLi: http://php.net/manual/en/book.mysqli.php
PDO: http://php.net/manual/en/ref.pdo-mysql.php
Error reporting: http://php.net/manual/en/function.error-reporting.php
Footnotes:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Make sure your server is running and properly configured for PHP/MySQL/Apache.
Make sure your columns are indeed VARCHAR and long enough to accommodate the data.

Try this query:
INSERT INTO guest set username=$value1,email=$value2
hope it will solve .

Try this, it should work for you.
$sql = "INSERT INTO guests (username, email) VALUES (".$value.", ".$value2.")";

Related

HTTP Error 405.0 - Method Not Allowed

I have a have rented a server at www.unoeuro.com (I dont know if this matters)
When i submit the form it gives me "HTTP Error 405.0 - Method Not Allowed"
<?php
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '')
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_slect_db(DB_NAME, $link);
if (!$db_selected) {
die('Could not connect: ' .mysql_error());
}
$value = $_POST['username'];
$sql = "INSERT INTO test (username) VALUES ('$value')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
<!doctype HTML>
<form action="test.php" method="post">
<p>Username: <input type="text" name="username" /><p>
<input type="submit" value="Submit" />
</form>
Can anyone tell me what i am doing wrong?
Just ask me for screenshots of anything you can use to help!
Sorry if the issue is obvious. This is my first time working with any kind of database, PHP and such.
Thanks!
First of all, if those are your actual database username and password, you're going to want to be changing those sharpish before someone else changes them for you :)
It's most likely down to how your server is configured to handle PHP files.
The first thing I would check is that the package you are leasing is designed to host PHP scripts. Looking at the UnoEuro package details (https://en.unoeuro.com/products.php#specs) it states 'ASP or PHP'. Is it possible that you are using a server setup for ASP?
If you are definitely on a PHP package, I would contact the service provider. They should know everything there is to know about configuring your server.
Also; there is a typo in your code 'mysql_slect_db'.

My form is submitting multiple database entries and I don't want it to

I have a simple html form and a php file to execute a database insertion. The problem I am having is that when I press the submit button, my database table receives 3 copies of the same submission and I only need one. Below is the code.
html:
<!DOCTYPE html>
<html>
<form action="demo.php" method="post">
<p>
Input 1: <input type="text" name="input1" />
<input type="submit" value="Submit" />
</p>
</form>
</html>
php:
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$values')";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
The DB_NAME, DB_USER, and DB_PASSWORD have all been changed for obvious reasons, but the code does work.
It just submits too many copies of the form data to the database table. Way back when I was in school, I had this issue, but it seemed like the problem was on the server's end and not our code. The server used here is mine and I do have full control over it. If the server is the issue at fault, I need help correcting that (as I am doing this to learn how to admin these tools, I do not know much more than basic level administration).
Kenneth, the code you have provided here honestly needs some work. First of all, please don't use the mysql API anymore. It's deprecated, will no longer be supported in future PHP versions, and is insecure. For all database operations use the mysqli or PDO API's, preferrably with prepared statements.
Secondly, do not ever INSERT $_POST or $_GET variables directly into the database without validating/sanitizing them first as someone could delete your data or even worse your whole database. PHP has numerous functions to make this very easy such as ctype depending on the data type.
Maybe try something like this in your code:
if (!empty($_POST['input1'])) { //checks if data was received//
$value = $_POST['input1'];
mysql_real_escape_string($value);
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
} else {
echo "form was not received";
exit;
}
I also noticed that your variable names were different, which is corrected above.
EDIT :
Mistakenly used wrong syntax for PHP ctype function.
You are taking the POST input value in the variable named $value and in query you are sending $values
I have corrected the code.
Can you please try the below code
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
if($value!=''){
$sql = "INSERT INTO demo (input1) VALUES ('".$value."')";
}
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
Below is correct code for the issue. I have checked that when you refresh your page it will create new blank entry in database and also the variable name is wrong.
You have to check for the Request method. This
$_SERVER['REQUEST_METHOD'] === 'POST'
will check the form method and it will prevent the blank entries in database.
<?php
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', 'mysqldba');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
//Test for request method
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
//echo $sql;die;
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
}
mysql_close();
?>

Syntax error on MySQL

we are working on a school project. We are trying to use some simple MySQL to store data from a single HTML text form into a MySQL database.
My HTML looks like this:
<form action="sql/tilmeld-sms.php" method="post">
<h2>Tilmelding til SMS:</h2>
<input type="text" name="sms">
<input type="submit" value="Submit">
</form>
Our SQL looks like this:
<?php
$connect = mysql_connect(“localhost”, “ODBC”, “”); if (!connect) { die('Connection Failed: ' . mysql_error()); { mysql_select_db(“database_name”, $connect);
$user_info = “INSERT INTO sms (sms) VALUES ('$_POST[sms]')”; if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error()); }
echo “Your information was added to the database.”;
mysql_close($connect);
?>
When we run this on our localhost, we get a syntax error on line 4. Which means there is something wrong with this line of code:
$user_info = “INSERT INTO sms (sms) VALUES ('$_POST[sms]')”; if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error
We know this is very simple MySQL. But its the first time we use it, and the first time we try to store data from a HTML form into a mysql database.
Other info:
Databasename: projekt32
Databasehost: localhost
Username: ODBC (We read this is the general username for localhost on
Windows)
Password: no password on localhost we read
Tablename: sms
EDIT: This is the error code we get:
Parse error: syntax error, unexpected 'INTO' (T_STRING) in C:\xampp\htdocs\projekt-3-2\sql\tilmeld-sms.php on line 4
EDIT2: This is a school project, and MySQL is part of this project. We wont pass without using it, so suggesting other things we can do than MySQL wont work but thanks anyway!
You are using back ticks instead of double quotes/single quotes.
UPDATED:
$connect = mysql_connect('localhost', 'ODBC', '');
if (!connect) {
die('Connection Failed: ' . mysql_error());
} // ALSO Use } instead of { here.
mysql_select_db('database_name', $connect);
ALSO,
$user_info = "INSERT INTO sms (sms) VALUES ('".$_POST['sms'] ."')";
Try this
<label for="sms">SMS</label>
<input id="sms" type="text" name="sms" />
<input type="submit" name="add" id="add" value="Go">
$sms = kontroll_form_data_string($_POST['sms'], 100);
if ( isset( $_POST['add'] ) ) {
$db = mysql_connect('localhost', 'ODBC', '');
mysql_select_db("projekt32") or die(mysql_error());
$insert = "INSERT INTO sms(sms) VALUES('$sms')";
if(mysql_query($insert)) {
echo("Done");
}
}
I think , you should try "mysql_real_escape_string", Concrete example of where mysql_real_escape_string fails and Prepared Statements are necessary
$connect = mysql_connect(“localhost”, “ODBC”, “”); if (!connect) { die('Connection Failed: ' . mysql_error()); { mysql_select_db(“database_name”, $connect);
$sms = mysql_real_escape_string($_POST[sms]);
$user_info = “INSERT INTO sms (sms) VALUES ('$sms')”; if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error()); }
echo “Your information was added to the database.”;
mysql_close($connect);
I hope this helps you.

Wamp server error message: Undefined variable: _post?

I am trying to link a HTML form to a mySQL database using a wamp server, the server connects to the database perfectly however it wont let me post my form data due to a error message Undefined variable: _post can somebody help solve whats going wrong below is my PHP code.
<?php
define ('DB_NAME', 'feedback');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('could not connect' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('cant use' . DB_NAME . ':' . mysql_error());
}
$_value = $_post['name'];
$_value1 = $_post['feedback'];
$sqlname = "INSERT INTO feedback (name) VALUES ('$_value')";
if (!mysql_query($sqlname)){
die('error:' . mysql_error());
}
$sqlfeedback = "INSERT INTO feedback (feedback) VALUES ('$value1')";
if (!mysql_query($sqlfeedback)){
die('error:' . mysql_error());
}<?php */
mysql_close();
?>
Variables are case sensitive in php, so it should be $_POST['name']and not$_post['name'].
Keep in mind tho, fonctions aren't case sensitive.

Cant see data after importing in _post form

Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli

Categories