Data is not posting in database - php

i am new to php and started developing one dynamic website, i have created few static pages and one news page.
For news page i have created database in phpmyadmin but i am not able to get any data in my database but i am getting images in images folder, please have look in my codes.
to insert post i have created this :
<html>
<head>
<title>Insert New Post</title>
</head>
<body>
<form method="post" action="insert_post.php" enctype="multipart/form-data">
<table allign="center" border="10" width="600">
<tr>
<td align="center" colspan="5" bgcolor="yellow">
<h1>Insert New Post Here</h1></td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="40"></td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td align="right">Post image:</td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td align="right">Post content:</td>
<td><textarea name="content" cols="40" rows="20"></textarea></td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include('includes/connect.php');
if(isset($_POST['submit'])){
$title = $_POST['title'];
$date = DATE('y-m-d');
$author = $_POST['author'];
$content = $_POST['content'];
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp = $_FILES['image']['tmp_name'];
if($title == '' or $author =='' or $content ==''){
echo "<script>alert('Any filed is empty')</script>";
exit();
}
if($image_type=="image/jpeg" or $image_type=="image/png" or $image_type=="image/gif"){
if($image_size<=50000){
move_uploaded_file($image_tmp,"images/$image_name");
}
else {
echo "<script>alert('image is larger, only 50kb size is allowed')</script>";
}
}
else {
echo "<script>alert('image type is invalid')</script>";
}
$query = "insert into post (post_title,post_date,post_author,post_image,post_content) values ('$title','$date','$author','$image_name','$content')";
if(mysql_query($query)){
echo "<center><h1>Post has been Published</h1></center>";
}
}
?>
Now i have created connect.php files, witch have following codes:
<?php
mysql_connect("localhost","root","");
mysql_select_db("rect");
?>
i am new to php so sorry if i did anything wrong in question and thank you in advance.

Note:
Make sure you have a table named post with this corresponding column names: post_title, post_date, post_author, post_image, post_content
And as suggested by #spencer7593, I'll try to convert your code to mysqli_* as their is a problem with your connection, and also to prevent SQL injections.
If you're gonna insert date into your database, instead of:
$date=DATE("y-m-d"); /* YY-MM-DD */
you should do:
$date=DATE("Y-m-d"); /* YYYY-MM-DD */
First is we fix your connection to your database (connect.php):
<?php
$mysqli = new mysqli("localhost", "root", "", "rect");
/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
Then change your insert query like this simple example:
$stmt = $mysqli->prepare("INSERT INTO post (post_title, post_date, post_author, post_image, post_content) VALUES (?,?,?,?,?)");
$stmt->bind_param('sssss',$title,$date,$author,$image_name,$content); /* BIND VARIABLES TO THE QUERY */
$stmt->execute(); /* EXECUTE QUERY */
?>

Related

PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Please help not able to resolve, trying since three days but not able to know what the reason its throwing me such message. (below is my complete code).
<!DOCTYPE html>
<html>
<head>
<title>Insert New Post</title>
</head>
<body>
<form method="post" action="insert_post.php" enctype="multipart/form-data">
<table align="center" border="10" width="600">
<tr>
<td align="center" colspan="5" bgcolor="yellow"><h1>Insert New Post Here</h1></td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="40"></td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td align="right">Post image:</td>
<td><input type="file" name="image_name"></td>
</tr>
<tr>
<td align="right">Post Content:</td>
<td><textarea name="content" cols="50" rows="20"></textarea></td>
</tr>
<tr>
<td align="center" colspan="5"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
</form>
</body>
</html>
Above Form is to insert and submit data in Database
// PHP
<?php
include("includes/connect.php");
if (isset($_POST['submit'])) {
$title = $_POST['title'];
$datenow = date('Y/m/d');
$author = $_POST['author'];
$content = $_POST['content'];
$image_name = $_FILES['image_name']['name'];
$image_type = $_FILES['image_name']['type'];
$image_size = $_FILES['image_name']['size'];
$image_tmp = $_FILES['image_name']['tmp_name'];
if ($title =='' || $author =='' || $content =='') {
echo "<script>alert('Any feild is empty')</script>";
exit();
}
if ($image_type =='image/jpeg' || $image_type =='image/png' || $image_type =='image/gif') {
if ($image_size<=5000000000) {
move_uploaded_file($image_tmp, "images/$image_name");
}
else{
echo "<script>alert('Image is larger, only 50kb size is allowed') </script>";
}
}
else{
echo "<script>alert('image type is invalid')</script>";
}
// insert query
$sth = $con->prepare(" INSERT INTO posts (post_title, post_date, post_author, post_image, post_content) VALUE (:title,:datenow,:author,:image_name,:content) ");
$sth->bindParam(':post_title', $title);
$sth->bindParam(':post_date', $datenow);
$sth->bindParam(':post_author', $author);
$sth->bindParam(':post_image', $image_name);
$sth->bindParam(':post_content', $content);
$sth->execute();
echo "<h1>Form Submited Successfully</h1>";
}
?>
// $sth->execute(); is throwing error massage as above
You are binding wrong values.You haven't bind the post_* values. See below:
$sth = $con->prepare(" INSERT INTO posts (post_title, post_date, post_author, post_image, post_content) VALUES (:post_title,:post_date,:post_author,:post_image,:post_content) ");
$sth->bindParam(':post_title', $title);
$sth->bindParam(':post_date', $datenow);
$sth->bindParam(':post_author', $author);
$sth->bindParam(':post_image', $image_name);
$sth->bindParam(':post_content', $content);

unexpected error message in php form (SQL syntax error)

I have made a simple php cms form with database but it does not work properly when I want to submit the form with some dummy data! I don't know why it happens & also I added the mysqli_error() to get the type of error that I'm facing with but I only got this:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '','','')' at line 2
<?php
if (isset($_POST['submit'])){
$post_title = $_POST['title'];
$post_date = date('d-m-y');
$post_author = $_POST['author'];
$post_keywords = $_POST['keywords'];
$post_content = $_POST['content'];
$post_image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
if ($post_title=='' or $post_keywords='' or $post_content='' or $post_author=''){
echo '<script>alert("Some fields are missing")</script>';
}else{
move_uploaded_file($image_tmp,"post_images/$post_image");
$insert_query = "INSERT INTO posts
(post_title,post_date,post_author,post_image,post_keywords,post_content) VALUES ('$post_title','$post_date','$post_author',$post_image','$post_keywords','$post_content')";
$insert_post = mysqli_query($con,$insert_query);
if ($insert_post){
echo '<h3 style="color:green">Post has been added successfully.</h3>';
}else{
echo mysqli_error($con);
}
}
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<table width="600" align="center" border="10">
<tr>
<td align="center"><h6>Insert Post Title</h6></td>
<td align="center"><input type="text" name="title"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Author</h6></td>
<td align="center"><input type="text" name="author"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Keywords</h6></td>
<td align="center"><input type="text" name="keywords"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Image</h6></td>
<td align="center"><input type="file" name="image"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Content</h6></td>
<td align="center"><textarea name="content" cols="10" rows="10"></textarea></td></br>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>
It would be very helpful to me if you share your solution for this problem... thanks!
You are missing a quote just before $post_image:
,$post_image'
Should be:
,'$post_image'
So the complete SQL statement becomes then:
$insert_query = "INSERT INTO posts
(post_title, post_date, post_author, post_image, post_keywords, post_content)
VALUES ('$post_title', '$post_date', '$post_author', '$post_image',
'$post_keywords', '$post_content')";
Please note that you are doing assignments in this if:
if ($post_title=='' or $post_keywords='' or $post_content='' or $post_author=''){
You should be using double == instead of =.
Finally, your code is vulnerable to SQL injection. So please use prepared statements with parameters.
writing if statement in this way is better
// this not always works
if ($post_title=='' or $post_keywords='' or $post_content='' or $post_author=''){
echo '<script>alert("Some fields are missing")</script>';
}
// yeah much better
if (empty($post_title) || empty($post_keywords) || empty($post_content) || empty($post_author)){
echo '<script>alert("Some fields are missing")</script>';
}
and sql mistake most probably because of here
'$post_keywords','$post_content')";
$post_keywords and $post_content is null or empty
Changes
Use empty for check empty variable
Use || instead of or
Check validation for what you are doing. (move_uploaded_file)
Be careful with quotes ($post_image') - This is the bug in your code
Enhance mysqli_error (if (!$insert_post){)
Code
<?php
if (isset($_POST['submit']))
{
$post_title = $_POST['title'];
$post_date = date('d-m-y');
$post_author = $_POST['author'];
$post_keywords = $_POST['keywords'];
$post_content = $_POST['content'];
$post_image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
if (empty($post_title) || empty($post_keywords) || empty($post_content) || empty($post_author))
{
echo '<script>alert("Some fields are missing")</script>';
}
else
{
if (!move_uploaded_file($image_tmp,"post_images/$post_image")) {
echo "Move Failed";
}
else
{
$insert_query = "INSERT INTO posts (post_title,post_date,post_author,post_image,post_keywords,post_content) VALUES ('$post_title','$post_date','$post_author','$post_image','$post_keywords','$post_content')";
$insert_post = mysqli_query($con,$insert_query);
if (!$insert_post){
echo mysqli_error($con);
}
else
{
echo '<h3 style="color:green">Post has been added successfully.</h3>';
}
}
}
}
?>

Insertion of data into Database

//Connection.php
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$db = 'faculty_corner';
$link = mysqli_init();
$success = mysqli_real_connect( $link, $host, $user, $password, $db);
if ($success == TRUE) {
echo 'KUDOS !!! Connected to Database';
}
?>
//varpay.php (the page from where I want the data to be sent to db)
//insert statement: in this form I have put fid as an autoincrement and calculation = NULL as I have a view button there which lets me to go to next page 'p1.php'
<pre>
<?php
if ($_POST['submit'] == 1){
$sql = "INSERT INTO `faculty_corner`.`tabdata` (fid, `facname`, `designation`, basic, varper, varamt, appamt, calculation) VALUES (NULL, '$_POST[facname]', '$_POST[designation]', $_POST[basic], $_POST[varper], $_POST[varamt], $_POST[appamt], NULL)";
if(mysqli_query($success, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
}
?>
</pre>
<form action="varpay.php" method="post">
<table name="myTable">
<thead>
<tr>
<th style="border-top-left-radius:10px;">S.no</th>
<th>Faculty Name</th>
<th>Designation</th>
<th>Basic AMt.</th>
<th>Var %</th>
<th>Var Amt.</th>
<th>Approved Aoount.</th>
<th style="border-top-right-radius:10px;">Calculation</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="width:60px;" name="fid">1</td>
<td><input name="facname" type="text" class="boxin" type="number"/></td>
<td align="center" style="width:60px;" ><input type="text" name="designation" class="boxin"/></td>
<td align="center"><input name="basic" class="boxin" type="number"/></td>
<td align="center"><input name="varper" class="boxon" type="number"/></td>
<td align="center"><input name="varamt" class="boxin" type="number" value="" /></td>
<td align="center"><input name="appamt" class="boxin" type="number" value="" /></td>
<td align="center"><input type="button" name="calculation" value="view"/></td>
</tr>
<tr><td colspan="8" align="center"><button type="submit" name="submit"> SUBMIT </button></td></tr>
</tbody>
</table>
</form>
This is my error.
First of all there is no need mentioning fid if it is an auto-increment column. Second of all you are inconsistent with using '.
Try replacing the query with this version:
$sql = "INSERT INTO `faculty_corner`.`tabdata` (facname, designation, basic, varper, varamt, appamt) VALUES ('$_POST[facname]', '$_POST[designation]', '$_POST[basic]', '$_POST[varper]', '$_POST[varamt]', '$_POST[appamt]')";
Change all post values $_POST[SomeName] to $_POST['SomeName']. You are missing ' inside square bracket [].
For auto increment, just change column fid to primary key and auto-increment in your database table. It will automatically get increment.
I've updated my code. Please have a look.
<?
$sql = "INSERT INTO `faculty_corner`.`tabdata` (`fid`, `facname`, `designation`, `basic`, `varper`, `varamt`, `appamt`, `calculation`)
VALUES (NULL, '".$_POST['facname']."', '".$_POST['designation']."', '".$_POST['basic']."', '".$_POST['varper']."', '".$_POST['varamt']."', '."$_POST['appamt']".', NULL)";
?>

Adding Data to mySql

I have a form and trying to insert data to the mysql database. but it always jump into the error.
Same database connection working fine to view data already in the database.
Database Connection for the page stored in a separate file :
<?php
$host ="localhost";
$user = "CENSORED";
$password = "CENSORED";
$link = mysql_connect($host,$user,$password) or die("An error occurred while connecting...");
//Database Selection
$dbname="CENSORED";
mysql_select_db($dbname);
?>
HTML Form
<form action="add_admin.php" method="post">
<table>
<tr>
<td>Email Address :</td>
<td><input id="admin_email" name="admin_email" type="text" size="20"</></td>
</tr>
<tr>
<td>Name :</td>
<td><input id="admin_name" name="admin_name" type="text" size="20"</></td>
</tr>
<tr>
<td>Mobile :</td>
<td><input id="admin_mobile" name="admin_mobile" type="text" size="12"</></td>
</tr>
<tr>
<td>Address :</td>
<td><textarea id="admin_address" name="admin_address" rows="4" cols="50"/> </textarea></td>
</tr>
<td>Password :</td>
<td><input id="admin_pw" name="admin_pw" type="text" size="20"</></td>
</tr>
<td><input type="reset" value="Reset"></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
PHP Code
<?php
$admin_email=$_POST['admin_email'];
$admin_name=$_POST['admin_name'];
$admin_mobile=$_POST['admin_mobile'];
$admin_address=$_POST['admin_address'];
$admin_password=$_POST['admin_password'];
$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";
if( mysql_query($link,$sql))
{
echo "Records Added";
}
else
{
echo "ERROR";
mysql_error($link);
}
mysql_close($link);
?>
Thanks in advance.
you have to include your Database connection file which you have kept as separate file in your php file.
<?php
include("dbconnection filename.php"):// this line.
$admin_email=$_POST['admin_email'];
$admin_name=$_POST['admin_name'];
$admin_mobile=$_POST['admin_mobile'];
$admin_address=$_POST['admin_address'];
$admin_password=$_POST['admin_password'];
$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";
if( mysql_query($link,$sql))
{
echo "Records Added";
}
else
{
echo "ERROR";
mysql_error($link);
}
mysql_close($link);
?>
Change to this
$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('".$admin_email."','".$admin_name."','".$admin_mobile."','".$admin_address."','".$admin_password."')";
use mysql_real_escape_string
$admin_email=mysql_real_escape_string($_POST['admin_email']);
$admin_name=mysql_real_escape_string($_POST['admin_name']);
$admin_mobile=mysql_real_escape_string($_POST['admin_mobile']);
$admin_address=mysql_real_escape_string($_POST['admin_address']);
$admin_password=mysql_real_escape_string($_POST['admin_password']);
You have problems with connecting to a database. I don't like your approach to of connecting to a database so i'll provide mine approach (which works so far).
Your database config should look like
class DataBaseClass{
public $_host = "localhost";
public $_user = "X32284679";
public $_database = "X32284679";
public $_pass = "X32284679";
function connectToDatabase(){
$conn = new mysqli($this->_host, $this->_user, $this->_pass, $this->_database);
$conn->set_charset("utf8");
return $conn;
if(! $conn) {
echo "Problems with connecting to database!";
exit;
}
}
}
Later on in some other code you use this file like this
require('nameOfFile.php');
$db = new DataBaseClass();
$mysqli=$db->connectToDatabase();
$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";
if($rs = $mysqli->query($sql)) {
//inserted
else {
//not inserted
$mysqli->close();
}
And so on, try this approach and see if it helps you.
In your PHP page you should include your connection file:
require_once('yourdbconnection.php');
And change $_POST['admin_password'] to $_POST['admin_pw'] according to your HTML.
HTML
<form action="add_admin.php" method="post">
<table>
<tr>
<td>Email Address :</td>
<td><input id="admin_email" name="admin_email" type="text" size="20"></td>
</tr>
<tr>
<td>Name :</td>
<td><input id="admin_name" name="admin_name" type="text" size="20"></td>
</tr>
<tr>
<td>Mobile :</td>
<td><input id="admin_mobile" name="admin_mobile" type="text" size="12"></td>
</tr>
<tr>
<td>Address :</td>
<td><textarea id="admin_address" name="admin_address" rows="4" cols="50"> </textarea></td>
</tr>
<td>Password :</td>
<td><input id="admin_pw" name="admin_pw" type="text" size="20"></td>
</tr>
<td><input type="reset" value="Reset"></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
PHP
<?php
require_once('yourdbconnection.php');
$admin_email=$_POST['admin_email'];
$admin_name=$_POST['admin_name'];
$admin_mobile=$_POST['admin_mobile'];
$admin_address=$_POST['admin_address'];
$admin_password=$_POST['admin_pw'];
$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";
mysqli_query($link, $sql) or die("Error: " . mysqli_error($link));
mysqli_close($link);
?>
This works for me. If it doesn't for you then:
Check if query columns match table columns
Check if you are using the right database and the right table
Check if you are checking result on the right database and the right table
Hope this helps!
EDIT
NOTE: I highly suggest you to switch from mysql to mysqli since mysql is now deprecated.
As you asked me to help out in one of my previous answers i decided to do some fancy stuff with this code :)
Remember, the db rows need to be named the same as your form name="name" for this to work!
db_connect.php:
$dbhost = ""; // this will ususally be 'localhost', but can sometimes differ
$dbname = ""; // the name of the database that you are going to use for this project
$dbuser = ""; // the username that you created, or were given, to access your database
$dbpass = ""; // the password that you created, or were given, to access your database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('An error occured while connecting to: '. $dbhost.' as: '.$dbuser);
mysql_select_db($dbname, $conn) or die('Sorry, an error occured when selecting the database: '.$dbname);
form.php:
<form action="add_admin.php" method="post">
<table>
<tr>
<td>Email Address :</td>
<td><input id="admin_email" name="admin_email" type="text" size="20"</></td>
</tr>
<tr>
<td>Name :</td>
<td><input id="admin_name" name="admin_name" type="text" size="20"</></td>
</tr>
<tr>
<td>Mobile :</td>
<td><input id="admin_mobile" name="admin_mobile" type="text" size="12"</></td>
</tr>
<tr>
<td>Address :</td>
<td><textarea id="admin_address" name="admin_address" rows="4" cols="50"/> </textarea></td>
</tr>
<td>Password :</td>
<td><input id="admin_pw" name="admin_pw" type="text" size="20"</></td>
</tr>
<td><input type="reset" value="Reset"></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
add_admin.php:
include 'db_connect.php'; //include connection
//Why add all post thingys when you can do it dynamically ?
$i = count($_POST);
$e = 0;
//Do a foreach loop on all POSTS coming in to this file..
foreach($_POST as $Key => $Value){
//Add commas behind everything :)
if($e++ < $i - 1){
//Escaping all the strings:
$Rows .= mysql_real_escape_string($Key).", ";
$Values .= "'".mysql_real_escape_string($Value)."', ";
}
//if its the last one, dont add a comma behind!
else{
//Still escaping all the strings:
$Rows .= mysql_real_escape_string($Key);
$Values .= "'".mysql_real_escape_string($Value)."'";
}
}//end foreach loop
//Insert etc etc...
$sql = mysql_query("INSERT INTO admin($Rows) VALUES($Values)");
//If successful:
if(mysql_query($conn, $sql)){
echo "Records added.";
}
//Error ?
else{
echo "Sorry, an error occured while inserting to: ".$Rows;
echo "<br/>";
mysql_error($conn);
}
//Close connection:
mysql_close($conn);

Post Data from One PHP File to Another Gives Error

I have two php files, one file submits data to a second file for an update action into mysql database.
below is the code for the file that submits data
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
$server_name="localhost";
// Create connection
$con = new mysqli($server_name, $username, $password, $db_name , 3306);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result = $con->query($sql);
$rows = $result->fetch_assoc();
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
$con->close();
?>
the second file for the update action is presented below
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
$server_name="localhost";
// Create connection
$con = new mysqli($server_name, $username, $password, $db_name , 3306);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=$con->query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
the error page is presented below.
An suggestions to fix the problem
Well you haven't set those values yet that's why it's getting an error.
First you must wrap your second file to check if it has submitted the form. Then set those variables inside.
<?php
if(isset($_POST['Submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$id = $_POST['id'];
// rest of your code goes here
}
Make changes to your second file as
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$id = $_POST['id'];
// add you rest of code
}
You should define some variables before line
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
For example
$tbl_name = 'mytable';
$name = 'ABC';
$lastname = 'XYZ';
$email = 'abc#example.com';
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
User this at top of your request page.
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$id=$_POST['id'];
In your second file where you write sql query to update data,
You are using :-
undefine variable
name, lastname, email, id
get posted value in these variable like that, before sql query:-
$name= $_POST['name'];
$lastname= $_POST['lastname'];

Categories