Why is my code not storing data in the database? I have removed all errors that occurred.
<html><body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="sample";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
echo "error";
}
echo "Connected successfully";
if(isset($_POST['submit']))
if(isset($_POST['submit'])){$Name=$_POST['name'];$age=$_POST['age'];}
$sql = "INSERT INTO input1 (Name,age) VALUES (Name,age)";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
<form action="" method="post">
First name:<br>
<input type="text" name="name"><br>
Age:<br>
<input type="text" name="age">
<input type="submit" name="Submit">
</form>
</body></html>
You're not using the variables as the values to insert into the DB. You should use a prepared statement.
$sql = "INSERT INTO input1 (Name, age) VALUES (?, ?)";
$stmt = mysqli_prepare($conn, $sql) or die(mysqli_error($conn));
mysqli_stmt_bind_param($stmt, "si", $Name, $age);
if (mysqli_stmt_execute($stmt)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_stmt_error($stmt);
}
Also, all this code should be inside the if (isset($_POST['submit'])) block. Otherwise you'll try to insert empty variables into the database when the user hasn't submitted the form yet.
Related
Html code:
<input type="file" class="form-control" name="video[]" placeholder=" Enter Image file">
<div><h2>OR</h2></div>
<input type="text" class="form-control" name="yurl" placeholder=" Enter youtube embedded code">
<button type='submit' name="save" class='btn btn-success waves-effect waves-light'><i class='fa fa-upload'></i> Save</button>
php code:
<?php
if(isset($_POST['save'])){
$yurl = ($_POST['yurl']);
$new_data=mysqli_real_escape_string($connection, stripslashes($yurl));
echo $yurl;
foreach($_FILES["video"]["tmp_name"] as $key=>$tmp_name){
$temp = $_FILES["video"]["tmp_name"][$key];
$name = $_FILES["video"]["name"][$key];
if(empty($temp))
{
break;
}
move_uploaded_file($temp,"../uploads/galleryvideos/".$name);
$sql = "INSERT INTO gallaryvids (video ,youtube)
VALUES ('$name', '$yurl')";
if (mysqli_query($connection,$sql)) {
?>
<script>
window.location.href = "view_gal_video.php";
</script>
<?php
}
}
}
?>
Url which I want to insert: https://youtu.be/sA0-QXbLnaE
both video and link get inserted
when i only want to insert link not a video it fails but don't gives any error
Updated code with database connection.
if(isset($_POST['save'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$yurl = $_POST['yurl'];
//$video='test';
foreach($_FILES["video"]["tmp_name"] as $key=>$tmp_name){
$temp = $_FILES["video"]["tmp_name"][$key];
$name = $_FILES["video"]["name"][$key];
if(empty($temp))
{
break;
}
move_uploaded_file($temp,"../uploads/galleryvideos/".$name);
$sql = "INSERT INTO gallaryvids (video, youtube)VALUES ('$name', '$yurl')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
I am having an issue in inserting into a table. The connection file is correct and is coming from the header.php. There are no errors but when I go within the table no records are being inserted.
<?php
include('header2.php');
if(isset($_POST['done'])) {
$title = $_POST['title'];
$description = $_POST['description'];
$link = $_POST['link'];
$company = $_POST['company'];
$sql = "INSERT INTO placements (title, description, link, company)
VALUES ('$title', '$description', '$link','$company')";
// use exec() because no results are returned
echo "New record created successfully";
}
?>
<html>
<head>
<title> Add a Placement </title>
</head>
<body>
<form method="post">
<input type="text" name="title" placeholder="title">
<input type="text" name="description" placeholder="description">
<input type="text" name="company" placeholder="company">
<input type="text" name="link" placeholder="link">
<input type="submit" name="done">
</form>
</body>
</html>
You are not executing the query at all. I assume your database connection as below and run your query. It should work.
Tested.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['done'])) {
$title = $_POST['title'];
$description = $_POST['description'];
$link = $_POST['link'];
$company = $_POST['company'];
$sql = "INSERT INTO placements (title, description, link, company)
VALUES ('$title', '$description', '$link','$company')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
<?php
$con = mysqli_connect('localhost','root','');
if(!$con)
{
echo 'not connected to server';
}
if(!mysqli_select_db($con,'user_table'))
{
echo 'database not selected';
}
$mobilenumber = $_POST['mobilenumber'];
$operator = $_POST['operator'];
$state = $_POST['state'];
$sql = "INSERT INTO user_details (mobilenumber,operator,state) VALUES ('".$mobilenumber."','".$operator."','".$state."')";
if(!mysqli_query($con,$sql))
{
echo 'not inserted';
}
else
{
echo 'inserted';
}
//$select="insert into user_details (mobilenumber,operator,state) VALUES ('".$mobilenumber."".$operator."".$state."')";
//$sql=mysql_query($select);
//print '<script type="text/javascript">';
//print 'alert("the data is inserted.....")';
//print '<script>';
//mysql_close();
?>
<form method="post" action="insert.php" id="submitForm">
<select id="operator" type="select" ng-model="operatorName" ng-class="operatorError ? 'error-border':''" autocomplete="off" placeholder="Select operator" ng-click="showOperators($event);" name="operator" method=POST action=/insert.php class="form-control fetchoperators drpdwn-arrow ng-pristine ng-valid ng-touched" required>
<select name="state" class="state" id="state" required name="state" method="post" action="insert.php">
The Simplest way is to try to debug your query.
What i would have done is simply the process as much as i could firt.
do echo $sql;
Then copy the result and paste it in the my sql server if that works.
Then check your connection object apparently it's look right but the best way of doing this copy the following code. and try this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_table";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user_details (mobilenumber,operator,state) VALUES ('".$mobilenumber."','".$operator."','".$state."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
change the credentials and check the response and paste it in the comments then we will go along.
you should use
mysqli_query($con,$sql) or die((mysql_error());
it will show you error for which it is not inserting data in you table
My code is already inserting a data on the database, but only the Primary key(AUTO_INCREMENT) is the only adding. I can't get the date and the text.
Is there something wrong in my code?
Here is my code below:
HTML:
<form action="insertleave.php" method="post">
<label>Date Filed:</label>
<input type="date" name="datefiled">
<label>Date of Leave:</label>
<input type="date" name="leavedate">
</div>
<div class="medium-6 columns">
<label>Reason of Leave:</label>
<textarea rows="8" form="leaveform" name="reason"></textarea>
</div>
<input type="submit" class="expanded button" name="formSubmit" value="File Leave">
</form>
PHP:
<?php
$datefiled = $_POST['datefiled'];
$leavedate = $_POST['leavedate'];
$leavereason = $_POST['leavereason'];
$config = parse_ini_file("phpconfig.ini");
$conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['dbname']);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO leaves (ID, EmployeeID,DateFiled, LeaveDate, Reason)
VALUES
('$ID','$EmployeeID','$DateFiled','$LeaveDate','$Reason')";
if (mysqli_query($conn, $sql)) {
echo "OK!";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
In your text area, you given it a name "reason"
in your post variable, your value is "leavereason"
change the $leavereason = $_POST['leavereason']; to $leavereason = $_POST['reason'];
In you Reason of leave text area name of input is different.
Your variable name are different in your sql query and you are assigning to different variable.
Also your EmployeeID is empty here. there is no input for EmployeeID from html file or you should post it to php file.
Change you php code like this.
<?php
$datefiled = $_POST['datefiled'];
$leavedate = $_POST['leavedate'];
$leavereason = $_POST['reason'];
$config = parse_ini_file("phpconfig.ini");
$conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['dbname']);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO leaves (ID, EmployeeID,DateFiled, LeaveDate, Reason)
VALUES
('$ID','$EmployeeID','$datefiled','$leavedate','$leavereason')";
if (mysqli_query($conn, $sql)) {
echo "OK!";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
I am having trouble inserting data into the database 'justrated'. Once the user has entered their business name it should create a new entry in the table 'businesses'. For some reason I cannot get it so that the data is entered in the table. Any advice is gladly appreciated.
CODE:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
<input type="text" name="BusinessName" method="POST">
<input type="Submit" value="submit" name="submit" method="POST">
</form>
<?php
if (isset($_POST["submit"])){
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('".$_POST['BusinessName']."' )";
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
One of your problems is that $_POST['BusinessName'] is empty because the form was submitted using a GET request, not a POST request. The method=POST attribute goes on the <form> element. Eg:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit">
</form>
Also, you should escape the data properly before you insert it into the database:
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('" . $conn->real_escape_string ($_POST['BusinessName']) . "' )";
Furthermore, in these two lines:
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
you try to execute the same query twice using both the MySQL and MySQLi extension. You should remove the first line.
HTML Code
<form method="post" action="test1.php">
<input type="text" name="BusinessName" >
<input type="Submit" value="submit" name="submit" >
</form>
PHP Code
if (isset($_POST["submit"]))
{
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
Don't mix the mysql & mysqli....
Html:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit" >
</form>
Php:
Use
$conn->query($sql); not mysql_query()
hello please check this one i hope this working for you
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}