Unable to insert mysql database through form data - php

I have created a form where I want to insert mysql database when I submit the values but when I click on the submit button the database isnt updated.. dont know where I am going wrong in my code..
<html>
<head>
<title>Form Data</title>
</head>
<body>
<form action="form.php" method="post">
Server Name: <input type="text" name="server_name"> <br />
IP Address: <input type="text" name="ip_address"> <br />
Server Role: <input type="text" name="server_role"> <br />
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "abcx";
$dbname = "serverasset_inventory";
$connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
$sql = "INSERT INTO asset_inventory (server_name,ip_address,server_role)
VALUES ('$_POST[server_name]','$_POST[ip_address]'),'$_POST[server_role]')";
$state = mysqli_query($connection,$sql);
mysqli_close($connection);
}
?>
</body>
</html>
Any help?
-A

you are using a extra bracket between query and missing quote in variable's use like below
$sql = "INSERT INTO `asset_inventory` (`server_name`,`ip_address`,`server_role`)
VALUES ('".$_POST['server_name']."','".$_POST['ip_address']."','".$_POST['server_role']."')";

if you want to insert the fields
for example
your sql will be take in variables then insert it in sql
$server_name = $_POST['server_name'];
$ip_address = $_POST['ip_address'];
$server_role = $_POST['server_role'];
insert into asset_inventory (server_name,ip_address,server_role) VALUES ('$server_name','$ip_address','$server_role';

Related

php; mysql;How to insert user_id or date_created?

I made a simple registration form. How should i edit this form to add something like : user_id, or date_created?
When i add user_id column to PHPMyAdmin, the user can't register, but when i have only the values : 'First Name' ; 'Last Name' ; 'Email' ; 'Password' inside database ,everything works.
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
</head>
<body>
<?php
$lclhost = "localhost";
$pass = "";
$root = "root";
$database = "regis";
$con=mysqli_connect ("$lclhost", "$root", "$pass") or die("connection fail".mysql_error());
mysqli_select_db($con, $database) or die("database fail".mysql_error());
?>
<form method="get">
First Name : <input type="text" name="fname"><br>
Last Name : <input type="text" name="lname"><br>
Email: <input type="text" name="email"><br>
Password: <input type="Password" name="password">
<input type="submit" name="btnsubmit">
</form>
<?php
if (isset($_GET['btnsubmit']))
{
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$email = $_GET['email'];
$password = $_GET['password'];
$registration = "INSERT INTO tbl_info values ('".$fname."','".$lname."','".$email."','".$password."')";
mysqli_query($con,$registration);
echo "Succes!";
}
?>
</body>
</html>
Because of your query doesnt specify columns, mysql will only work if the number of column values you provided is exactly the same as the number of columns in the database.
Adding an extra column to the database will cause the query to break because the number of columns is not longer the same.
You have two options:
OPTION 1: Add column names in your query:
"INSERT INTO tbl_info(Firstname, Lastname, EmailAddress, Password)
values ('".$fname."','".$lname."','".$email."','".$password."')";
OPTION 2: Always make sure the number of columns in the database is exactly the same as the number of values you provide in your query
NB: If you have an auto-increament id field, you do not have to include it for the first option because it will auto-add value whenever you insert a record. But you do need to include it for the second option, pass empty string or null as a value

can't get connection with sqlite DB

I am quite familiar with PHP and mySQL. But for a change, I wanted to use sqlite as a DB, I just can't get the connection right. I have been following the 2. code example from this website, so my code looks like this:
<html>
<head>
<title>
DB bearbeiten
</title>
</head>
<body>
<form action="insert.php" method="post">
<input type="text" name="name" placeholder="Name">
<input type="text" name="eName" placeholder="E-Number">
<input type="text" name="causes" placeholder="Wirkung">
<input type="number" name="danger" placeholder="Gefahreinstufung">
<button type="submit" name="submit">Eintragen</button>
</form>
<?php
echo '1';
if(isset($_POST['submit'])){
echo '2';
$table = "zusatzstoffe";
$filename = "sqlite:/data/Adrian/zusatzstoffe.db";
$db = new PDO($filename) or die("cannot open DB");
//$db = SQLite3::_construct();
settype($danger, "integer");
$name = $_POST['name'];
$eName = $_POST['eName'];
$causes = $_POST['causes'];
$danger = $_POST['danger'];
$db->exec("INSERT INTO $table (name, number, causes, danger) VALUES ($name, $eName, $causes, $danger)");
}
?>
</body>
</html>
This line doesn't work, I just don't know why: $db = new PDO(filename, '','') or die("can not open DB");:

PHP insert data to Mysql

I am experimenting with PHP and Mysql. I have created a database and table at mu localhost using xampp. I have also created a file that suppose to populate my table by executing a query, but the strange thing is that i get no errors but at the same time no DATA has been inserted into my DataBase:
CODE:
register.php:
<?php
session_start();
if(isset($_POST['submitted'])){
include('connectDB.php');
$UserN = $_POST['username'];
$Upass = $_POST['password'];
$Ufn = $_POST['first_name'];
$Uln = $_POST['last_name'];
$Uemail = $_POST['email'];
$NewAccountQuery = "INSERT INTO users (user_id,username, password, first_name, last_name, emial) VALUES ('$UserN','$Upass', '$Ufn', '$Uln', '$Uemail')";
if(!mysql_query($NewAccountQuery)){
die(mysql_error());
}//end of nested if statment
$newrecord = "1 record added to the database";
}//end of if statment
?>
<html>
<head>
<title>Home Page</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<header><h1>E-Shop</h1></header>
<article>
<h1>Welcome</h1>
<h1>Create Account</h1>
<div id="login">
<ul id="login">
<form method="post" action="register.php" >
<fieldset>
<legend>Fill in the form</legend>
<label>Select Username : <input type="text" name="username" /></label>
<label>Password : <input type="password" name="password" /></label>
<label>Enter First Name : <input type="text" name="first_name" /></label>
<label>Enter Last Name : <input type="text" name="last_name" /></label>
<label>Enter E-mail Address: <input type="text" name="email" /></label>
</fieldset>
<br />
<input type="submit" submit="submit" value="Create Account" class="button">
</form>
</div>
<form action="index.php" method="post">
<div id="login">
<ul id="login">
<li>
<input type="submit" value="Cancel" onclick="index.php" class="button">
</li>
</ul>
</div>
</article>
<aside>
</aside>
<div id="footer">This is my site i Made coppyrights 2013 Tomazi</div>
</div>
</body>
</html>
I have also one include file which is connectDB:
<?php
session_start();
$con = mysql_connect("127.0.0.1", "root", "");
if(!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("eshop", $con) or die("Cannot select DB");
?>
Database structure:
database Name: eshop;
only one table in DB : users;
users table consists of:
user_id: A_I , PK
username
password
first_name
last_name
email
I spend a substantial amount of time to work this out did research and looked at some tutorials but with no luck
Can anyone spot what is the root of my problem...?
It is because if(isset($_POST['submitted'])){
you dont have input field with name submitted give the submit button name to submitted
<input name="submitted" type="submit" submit="submit" value="Create Account" class="button">
Check your insert query you have more fields than your values
Change :
$NewAccountQuery = "INSERT INTO users (user_id,username, password, first_name, last_name, email) VALUES ('$UserN','$Upass', '$Ufn', '$Uln', '$Uemail')";
to :
$NewAccountQuery = "INSERT INTO users (user_id,username, password, first_name, last_name, email) VALUES ('','$UserN','$Upass', '$Ufn', '$Uln', '$Uemail')";
Considering user_id is auto increment field.
Your email in query is written wrongly as emial.
Is error reporting turned on?
Put this on the top of your screen:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Some good answers above, but I would also suggest you make use of newer MySQLi / PDO instead of outdated 2002 MySQL API.
Some examples: (i will use mysqli since you wrote your original example in procedural code)
connectDB.php
<?php
$db = mysqli_connect('host', 'user', 'password', 'database');
if (mysqli_connect_errno())
die(mysqli_connect_error());
?>
register.php -- i'll just write out an example php part and let you do the rest
<?php
//i'll always check if session was already started, if it was then
//there is no need to start it again
if (!isset($_SESSION)) {
session_start();
}
//no need to include again if it was already included before
include_once('connectDB.php');
//get all posted values
$username = $_POST['username'];
$userpass = $_POST['password'];
$usermail = $_POST['usermail'];
//and some more
//run checks here for if fields are empty etc?
//example check if username was empty
if($username == NULL) {
echo 'No username entered, try again';
mysqli_close($db);
exit();
} else {
//if username field is filled we will insert values into $db
//build query
$sql_query_string = "INSERT INTO _tablename_(username,userpassword,useremail) VALUES('$username','$userpass','$usermail')";
if(mysqli_query($db,$sql_query_string)) {
echo 'Record was entered into DB successfully';
mysqli_close($db);
} else {
echo 'Ooops - something went wrong.';
mysqli_close($db);
}
}
?>
this should work quite nicely and all you need to add is your proper posted values and build the form to post it, that's all.
<?php
$db = mysqli_connect('host', 'user', 'password', 'database');
if (mysqli_connect_errno())
die(mysqli_connect_error());
?>
register.php -- i'll just write out an example php part and let you do the rest
<?php
//i'll always check if session was already started, if it was then
//there is no need to start it again
if (!isset($_SESSION)) {
session_start();
}
//no need to include again if it was already included before
include_once('connectDB.php');
//get all posted values
$username = $_POST['username'];
$userpass = $_POST['password'];
$usermail = $_POST['usermail'];
//and some more
//run checks here for if fields are empty etc?
//example check if username was empty
if($username == NULL) {
echo 'No username entered, try again';
mysqli_close($db);
exit();
} else {
//if username field is filled we will insert values into $db
//build query
$sql_query_string = "INSERT INTO _tablename_(username,userpassword,useremail) VALUES('$username','$userpass','$usermail')";
if(mysqli_query($db,$sql_query_string)) {
echo 'Record was entered into DB successfully';
mysqli_close($db);`enter code here`
} else {
echo 'Ooops - something went wrong.';
mysqli_close($db);
}
}
?>

inserting data into mysql table

I have just installed xampp but I'm having problem inserting data into MySQL table.
The data I inserted does not appear in phpmyadmin. I am not sure what is the problem.
Any help will be gratefully appreciated, here is my code:
<html>
<head>
<title>Test</title>
<body>
<form action="test.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit"name="submit" value="submit"/>
</form>
<?php
$host ="localhost";
$username = "***";
$password = "***";
$database = "***";
$table ="persons";
$con = mysql_connect("localhost","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bucksbug_mesovot", $con);
$sql="INSERT INTO persons (firstname, lastname, age)
VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Data inserted";
mysql_close($con);
?>
</body>
</html>
Although you should really switch to PDO / mysqli and prepared statements with bound parameters to avoid sql injection and breaking sql statements if your variables contain quotation marks, you will probably run into problems with PDO / mysqli as well: Your password (change it!) contains a $.
See the following example on codepad:
<?php
function test($var)
{
echo $var;
}
$test_var = "test$string";
test($test_var);
Output:
test
You will not be able to connect successfully to the database as your password will never be correct.
Change:
"=c(p$zTTH2Jm"
to:
'=c(p$zTTH2Jm'

Connecting Php, html, and mysql

Can someone look over my code and let me know what's wrong with it?
The problem I'm having is that when I enter text to the 3 fields and hit submit, it doesn't insert to my database (mysql, with phpmyadmin as gui). No error messages or anything; it simply doesn't insert the data..
I have looked over the code over and over, and I can't pin point what's wrong with it.
//---------------------------This is my index.php------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Web Bar Title</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<?php
if(isset($_POST['Submit']))
{
include 'connectdb.php';
include 'openconnection.php';
$first = $_POST['first'];
$second = $_POST['second'];
$third = $_POST['third'];
$query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third');";
mysql_query($query) or die('Error, insert query failed');
}
?>
<div id="page">
<tbody>
<form method="post">
<table>
<tr>
<td ><b>First</b></td>
<td><input name="first" type="text" id="first"></td>
<tr>
<tr>
<td ><b>Second</b></td>
<td><input name="second" type="text" id="second"></td>
<tr>
<td ><b>Company</b></td>
<td><input name="third" type="text" id="third" > </td>
</tr>
</table>
<input name="submit" type="submit" id="submit" value="Submit" />
</form>
</body>
</html>
</tbody>
</div>
//---------------------------------connectdb.php------------------------------------------
<?php
$dbhost = 'localhost';
$dbuser = 'sharkk';
$dbpass = 'pw';
$dbname = 'test';
?>
//---------------------------------openconnection.php-------------------------------------
<?php
$dbhost = 'localhost';
$dbuser = 'sharkk';
$dbpass = 'pw';
$dbname = 'test';
?>
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname) or die ('No Selection of Database');
?>
EDIT: It would be easier and faster to communicate via MSN/AIM/Steam/Skype, if anyone has any of those!
Change your top line to
if( isset( $_POST['submit'] ) ) {
I can't remember if it is case sensitive or not
Better still change it to
if($_SERVER['REQUEST_METHOD'] == 'POST')
The isset() method on the submit button is unreliable because Internet Explorer will not send the submit button as a post variable if the user presses the enter key to submit the form, and thus your code will not detect a form submission.
Just check MYSQL INSERT query in Your own MySQL platform, by simply copying your INSERT query and paste in your MySQL database, and check there. it wil report your fault.
If I'm right, then your problem lies on your INSERT query part. You have stated :
$query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third');";
In the above part, there shouldn't be 2 semicolons, Jus one is enough... it wil look like :
$query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third')";
Also, check ur include part too...
it shouldnt be :
include 'connectdb.php'; //braces r missing
include 'openconnection.php'; // braces r missing
it should be :
include ('connectdb.php');
include ('openconnection.php');
I hope this may do good fa U ....
CHEERS buddy.........
Try putting the values between double quotes instead of single quotes
VALUES("$first","$second","$third")

Categories