nothing being added to the database - php

<?php
if(isset($_POST['sub']))
{
$mname=$_POST['sub'];
}
if(isset($_POST['pos']))
{
$pos=$_POST['pos'];
}
if(isset($_POST['rad1']))
{
$vis=$_POST['rad1'];
}
global $mname, $pos, $vis;
$q= "INSERT INTO subjects (menu_name, position, visible) VALUES ('$mname', '$pos', '$vis')";
$qs=mysql_query($q, $connection);
if($qs)
{
header("Location: content.php");
exit;
}
else{
echo mysql_error();
}
?>
I get sub, pos and rad1 from a form
my database's primary key is auto-incrementing but rows are empty
Where is the mistake?

It seems like your $_POST array is empty.
Do a
<?php
print_r($_POST);
exit();
?>
This way we can know if are you getting an empty $_POST.

Try this code it should be working properly
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$mname='abc';
$pos='13';
$vis='true';
$q= "INSERT INTO subjects (`menu_name`, `position`, `visible`) VALUES ('".$mname."', '".$pos."', '".$vis."')";
$qs=mysql_query($q);
if($qs)
{
//header("Location: db.php");
exit;
}
else{
echo mysql_error();
}
?>

Related

UPDATE now getting no errors and 'success' but still no image in the data base. Updated code below

updated code and it's now displaying success but still no image in my database.
anyone have any idea why the image isnt being inserted?
<?php
error_reporting( E_ALL );
?>
<?php
if (isset($_POST['submit'])) {
if (getimagesize($_FILES['Image']['tmp_name'])==FALSE) {
echo "failed";
} else {
$name=addslashes($_FILES['Image']['name']);
$image=base64_encode(file_get_contents(addslashes($_FILES['Image']['tmp_name'])));
saveimage($name,$image);
}
} else {
echo "error";
}
function saveimage($name,$image) {
$con = mysqli_connect('', '','', '');
$sql = "INSERT INTO items ('image', 'Description') VALUES ($name, $image)";
$query=!mysqli_query($con,$sql);
if ($query) {
echo "success";
} else {
echo "Not uploaded";
}
}
?>
I have Found the issue
Please update your query using i have given
$sql = "INSERT INTO items (image, Description) VALUES ('".$name."', '".$image."')";
You should check you image column datatype in database table this should be text or blob type.

issues with mysqli_insert_id

I'm trying to use mysqli_insert_id in multiple queries, but I keep getting Cannot add or update a child row: a foreign key constraint fails. Below is my code:
$con=mysqli_connect("Stuff");
if(mysqli_connect_errno()){
echo "There was a mistake connecting". mysqli_connect_errno();
}
$First=mysqli_real_escape_string($con,$_POST["FirstName"]);
$Last=mysqli_real_escape_string($con, $_POST["LastName"]);
$Phone=mysqli_real_escape_string($con,$_POST["Number"]);
$Product=mysqli_real_escape_string($con,$_POST["Product"]);
$Quantity=mysqli_real_escape_string($con,$_POST["Quantity"]);
if(!empty($_POST["FirstName"]) && !empty($_POST["LastName"])){
$sql="INSERT INTO Customer(First,Last)
VALUE('$First', '$Last')";
$id = mysqli_insert_id($con);
if(!mysqli_query($con,$sql)) {
die("ERROR". mysqli_error($con));
}
}
if(!empty($_POST["Number"])){
$sql="INSERT INTO Customer_Number(Customer_ID,Number)
VALUE('$id','$Phone')";
if(!mysqli_query($con,$sql)) {
die("ERROR". mysqli_error($con));
}
}
if(!empty($_POST["Product"]) && !empty($_POST["Quantity"])){
$sql="INSERT INTO Product(Customer_ID,Product,Quantity)
VALUE('$id','$Product','$Quantity')";
if(!mysqli_query($con,$sql)) {
die("ERROR". mysqli_error($con));
}else{
echo "Special Order Added";
}
}
mysqli_close($con);
?>
I've also tried using Last_Insert_ID() as well, but that only works for one query, and then gives me the same error message when I try and add it to the 2nd one.
call mysqli_insert_id after the mysqli_query

PHP sending variable to other page

I have a form, on isset function data is inserted in db also it returns a variable named id from db. i want to post this variable to next page.
here is code;
if (isset($_POST['preview'])){
echo $user = $_SESSION['ue'];
echo $title=$_POST['title'];
echo $dis=$_POST['dis'];
echo $a=$_POST['a'];
echo $b=$_POST['b'];
echo $c=$_POST['c'];
echo $d=$_POST['d'];
echo $timespan=$_POST['timespan'];
$sql="INSERT INTO survey (user, title, description, opta, optb,optc,optd,time) VALUES ('$user','$title', '$dis', '$a' , '$b', '$c', '$d','timespan')";
if (mysqli_query($con,$sql))
{
echo "Success";
}
else
{
echo "Error: " . mysql_error();
}
$id = mysqli_insert_id($con); //variable to send to next page
mysqli_close($con);
}
?>
Thanks in advance :)
You can send it not next page using GET attribute in url as:
header("Location: http://mydomain.com/myOtherPage.php?id=".$id);
In myOtherPage you can use:
if(isset($_GET['id']))
{
$idFromPreviousPage=$_GET['id'];
}

empty field to mysql using php

i have code for save 3 textbox in one field in databse
no problem when i am enter 3 textbox , but when i fill 1 textbox and press ok
save another textbox in database as blank
i want just take the textbox is fulled and ignore the textbox empty
this is my code
<?php
include("connect.php");
$expert_name = trim($_POST['expert_name']);
$expert_name2 = trim($_POST['expert_name2']);
$expert_name3 = trim($_POST['expert_name3']);
// this is for arabic language.
mysql_query("SET NAMES utf8");
// Insert data into mysql
$sql="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')";
$sql2="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')";
$sql3="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')";
$result=mysql_query($sql);
$result2=mysql_query($sql2);
$result3=mysql_query($sql3);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
// echo "<a href='formadd.php'>Back to main page</a>";
}
else {
echo "ERROR";
echo "<br>";
// this for print error in insert process
echo mysql_error();
echo "<a href='expert_add.php'><br>Please try again </a>";
}
//mysql_close($con);
?>
back to form add
Execute your sql query only the variable value not equal to empty.
try this,
$expert_name = trim($_POST['expert_name']);
$expert_name2 = trim($_POST['expert_name2']);
$expert_name3 = trim($_POST['expert_name3']);
// this is for arabic language.
mysql_query("SET NAMES utf8");
// Insert data into mysql
if ($expert_name != "") {
$sql = "INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')";
$result = mysql_query($sql);
}
if ($expert_name2 != "") {
$sql2 = "INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')";
$result2 = mysql_query($sql2);
}
if ($expert_name != "") {
$sql3 = "INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')";
$result3 = mysql_query($sql3);
}
// if successfully insert data into database, displays message "Successful".
if ($result || $result2 || $result3) {
echo "Successful";
echo "<BR>";
// echo "<a href='formadd.php'>Back to main page</a>";
} else {
echo "ERROR";
echo "<br>";
// this for print error in insert process
echo mysql_error();
echo "<a href='expert_add.php'><br>Please try again </a>";
}
//mysql_close($con);
?>
back to form add
You should also check $result2 and $result3. I added that in this answer
try this
if ( !empty($_POST['expert_name']) ){
$sql="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')";
$result=mysql_query($sql);
}
if ( !empty($_POST['expert_name2']) ){
$sql2="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')";
$result2=mysql_query($sql2);
}
if ( !empty($_POST['expert_name3']) ){
$sql3 ="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')";
$result3 =mysql_query($sql3 );
}
Then you might want to check if the variable is empty().
<?php
include("connect.php");
$expert_name = trim($_POST['expert_name']);
$expert_name2 = trim($_POST['expert_name2']);
$expert_name3 = trim($_POST['expert_name3']);
// this is for arabic language.
mysql_query("SET NAMES utf8");
// Insert data into mysql
if(!empty($expert_name)) {
$sql="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')";
$result=mysql_query($sql);
}
if(!empty($expert_name2)) {
$sql2="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')";
$result2=mysql_query($sql2);
}
if(!empty($expert_name3)) {
$sql3="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')";
$result3=mysql_query($sql3);
}
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
// echo "<a href='formadd.php'>Back to main page</a>";
}
else {
echo "ERROR";
echo "<br>";
// this for print error in insert process
echo mysql_error();
echo "<a href='expert_add.php'><br>Please try again </a>";
}
Also note: You only check if $result is okay. If you only fill textbox 2 and leave 1 empty, the value of 2 it will get inserted but an error is shown.
I'd say your code need general review, but as it is for now you will have to do something like this each query:
if (!empty($expert_name2){
$result2=mysql_query($sql2)
}
But you should try to loop your queries in foreach rather than manually write every on query. And by the way:
if($result){
echo "Successful";
echo "<BR>";
// echo "<a href='formadd.php'>Back to main page</a>";
}
This code only return succes when 1st wuery success because you use $result which is set in 1st query only
The ID is probably NOT NULL AUTO_INCREMENT, so that won't accept NULL as value.
try sending blank value, such as:
$sql="INSERT INTO experts(id,expert_name) VALUES ('', '$expert_name')";
Also, build bulk insert, rather than multiple.
I will explain why, when you insert single insert into the database, the values being inserted, then, the DB engine flushes indexes (they written to disk), unless you have set delay_key_write=ALL in you my.cnf. Index flushing directly affects your db performance.
Please, check the reworked code out. The code adjusted for bulk insert, sql string escaping for security purposes and additional verification for post keys existence.
<?php
include("connect.php");
// this is for arabic language.
mysql_query("SET NAMES utf8");
$values = array();
$skipInsert = true;
$fields = array('expert_name', 'expert_name2', 'expert_name3');
$insert = "INSERT INTO experts(id,expert_name) VALUES ";
// Loop through predefined fields, and prepare values.
foreach($fields AS $field) {
if(isset($_POST[$field]) && !empty($_POST[$field])) {
$values[] = "('', '".mysql_real_escape_string(trim($_POST[$field]))."')";
}
}
if(0 < sizeof($values)) {
$skipInsert = false;
$values = implode(',', $values);
$insert .= $values;
}
if(false === $skipInsert) {
mysql_query($insert);
}
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful","<BR>";
// echo "<a href='formadd.php'>Back to main page</a>";
} else {
echo "ERROR","<br>",mysql_error(),"<a href='expert_add.php'><br>Please try again </a>";
}
HTH,
VR
if(!empty($textbox1_value)) {
//DO SQL
}
You can repeat this for multiple boxes however you wish, the empty operator checks if its empty, so if its not empty the "//DO SQL" area will get run.

PHP query inserts 2 rows into the table

OK, here is the deal. I have 3 queries putting data in different tables. 2 of them are in included files. I've tried to put them into the main code, the result was the same. The first two queries inserts one row with the data and a blank row. The third works fine. Here is the code.
This in the main page:
<? $p=0;
if ($select2)
{
$event=$select2;
}
else
{
require_once("insert_gal_name.php5");
}
if ($select)
{
$folder=$select;
}
else
{
require_once("insert_folder.php5");
if (file_exists("Connections/".$folder))
{
}
else
{
mkdir("Connections/$folder",0777);
}
}
while($p<$number)
{
$p++;
$dir = 'Connections/'.$folder.'/'; // Директорията в която ще се записват файловете
copy($_FILES['file']['tmp_name'][$p],"$dir".$_FILES['file']['name'][$p]); //Копиране на файла
echo "Файлът бе качен успешно!<br>"; // Извеждане на съобщение показващо, че файла е качен
if($_FILES['file']['name'][$p])
{
$real_file_name = $dif_of_files.$_FILES['file']['name'][$p];
$nom_file = str_replace(" ", "", $real_file_name); }
$query = "INSERT INTO gallery (name, title, day, month, year) VALUES ('$nom_file', '$event', '$day', '$month', '$year')";
$result = mysql_query($query) or die(mysql_error());
}
if (!$result)
{
echo "Error";
}
else
{
echo "All data was uploaded successfuly.";
}
?>
And here are the includes in the order they are placed in the source
<? $query = "INSERT INTO gallery_names (name) VALUES ('$event')";
$result = mysql_query($query) or die(mysql_error());
if (!$result)
{
echo "Error";
}
else
{
echo "The gallery was created successfuly.";
}
?>
<? $query = "INSERT INTO folders (name) VALUES ('$folder')";
$result = mysql_query($query) or die(mysql_error());
if (!$result)
{
echo "Error";
}
else
{
echo "The folder was created successfuly.";
}
?>
<?
if($event!='')
{
$query1 = "INSERT INTO gallery_names (name) VALUES ('$event')";
$result1 = mysql_query($query1) or die(mysql_error());
if (!$result1)
{
echo "Error";
}
else
{
echo "The gallery was created successfuly.";
}
}
?>
If $select2 is false then this code will do two inserts. If $event is empty one of these will be an empty row which sounds like the problem you're having.
What are the values of $select2 and $event? Have you tried debugging them?

Categories