<?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
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();
}
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.
i try to creat a table with html and php
when i insert data into my db i get num 1 like a values in all column
this my code
<html dir="rtl">
<form action="" method="post">
<label for="Nom">الاسم:</label>
<center><input type="text" name="Nom"></center>
<label for="Cin">البطاقة الوطنية:</label>
<center><input type="text" name="Cin"></center>
<label for="Tel">الهاتف:</label>
<center> <input type="text" name="Tel"></center>
<label for="DATE_donation"> تاريخ التبرع:</label>
<center><input type="date" name="DATE_donation"></center>
<center><input type="submit" value="إدخال"></center>
</form>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ikhlas";
$con= mysqli_connect($servername, $username, $password, $dbname);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$Name =isset($_POST['Nom']);
$CIN = isset($_POST['Cin']);
$TEL = isset($_POST['Tel']);
$date = isset($_POST['DATE_donation']);
$sql="INSERT INTO persone(Nom, Cin, Tel, DATE_donation) value ('$Name','$CIN','$TEL','$date')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
?>
and this my ressult in dbenter image description here
Sure because you define all variable with $foo = isset($bar) instead of
if(isset($bar))
$foo = $bar;
Take a look to the doc about SQL injection too: http://php.net/manual/en/security.database.sql-injection.php
Remove your isset()
http://php.net/manual/en/function.isset.php
Replace to:
if(isset($_POST['إدخال']))
{
$Name = (!empty($_POST['Nom']))?$_POST['Nom']:"";
$CIN = (!empty($_POST['Cin']))?$_POST['Cin']:"";
$TEL = (!empty($_POST['Tel']))?$_POST['Tel']:"";
$date = (!empty($_POST['DATE_donation']))?$_POST['DATE_donation']:"";
}
I´m trying to create a form connected to a database but when I fill out the form and I refer to the table in phpMyAdmin I see that it have entered a blank record instead of form data. I´m using PhpStorm.
I think all this code is correct...
That is the form of the .html:
<form id="form1" name="form1" method="post" action="index.php">
<label for="userSignUp">Email</label>
<input type="text" name="userSign" id="userSignUp" />
<label for="passwordSignUp">Password</label>
<input type="password" name="passwordSign" id="passwordSignUp" />
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form>
I have the following .php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db_selected = mysqli_select_db($conn, $dbname);
$userSignUp = ""; // If I substitute "" with characters at this time the table is well updated
$passwordSignUp = ""; // Same as before
if(isset($_POST['userSign'])){
$userSignUp = $_POST['userSign'];
}
if (isset($_POST['passwordSign'])) {
$passwordSignUp = $_POST['passwordSign'];
}
$sql = "INSERT INTO test.person (FirstName, Password) VALUES ('$userSignUp', '$passwordSignUp')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
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);
?>