For some reason my query isn't working, I'll post the code and output of $_POST.
I will also add a screenshot for the structure of the database.
$_POST:
Code:
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "<pre>";
var_dump($_POST);
echo "</pre>";
$mysqli = new mysqli("localhost", "lunar_casino", "******", "lunar_casino");
if(isset($_POST['submit'])){
$error = array();
if(empty($error)){
$bonus = $_POST['bonus'];
$deposit = $_POST['deposit'];
$offers = $_POST['offers'];
$link = $_POST['link'];
$name = $_POST['logo'];
$q = $mysqli->query("INSERT INTO `lunar_casino`.`casino` VALUES(NULL, '$bonus', '$deposit', '$offers', '$link', '$logo', '$name', NULL)");
if(!$q){
echo "<font color='red'><b>There has been an error with our database! Please contact the website administrator!</b></font><br /><br />";
} else {
echo "<font color='green'><b>You have successfully added the casino!</b></font><br /><br />";
}
} else {
echo "<font color='red'><b>There were ".count($error)." errors in your form:</b></font><br />";
foreach($error as $err){
echo "<b>".$err."</b><br />";
}
echo "<br />";
}
}
Structure of Database:
If you need more information just let me know!
By the way, the way I know the error is in the query is because I checked if(!$q) and made it display an error message if the query can't be done, and it displays the error message on the page.
Any ideas why its not working? Also I left out date from the query because I don't know how to add the current date:time into the query.
If anyone could help with either of these issues please let me know! :)
I think the problem is that you set NULL the date column. Try NOW() instead of NULL:
$q = $mysqli->query("INSERT INTO `lunar_casino`.`casino` VALUES(NULL, '$bonus', '$deposit', '$offers', '$link', '$logo', '$name', NOW())");
Your error here is isn't very clear. Here are some debugging options you can try:
Name the fields
$q = $mysqli->query("INSERT INTO `lunar_casino`.`casino` VALUES(NULL, '$bonus', '$deposit', '$offers', '$link', '$logo', '$name', NULL)");
to
$q = $mysqli->query("INSERT INTO `lunar_casino`.`casino`(field1, field2...) VALUES(NULL, '$bonus', '$deposit', '$offers', '$link', '$logo', '$name', NULL)");
Save your query to a string and print it out. (Gordon Linoff)
echo "INSERT INTO `lunar_casino`.`casino` VALUES(NULL, '$bonus', '$deposit', '$offers', '$link', '$logo', '$name', NULL)"
before the query. This can show you what PHP is making for MySQL to feed on.
Check for errors
if ($mysqli->connect_errno) { //check connection error
printf("Connect failed: %s\n", $mysqli->connect_error); //check query error
exit();
}
Related
i am adapting this code to Mysqli, but is gives an error, i cannot see the error, please help.
$sql = "INSERT INTO test_xml ('title', 'artist', 'duration') VALUES ('$title', '$artist', '$duration')";
$result = mysqli_query($con, $sql);
the old code worked good:
$sql = "INSERT INTO `test_xml` (`title`, `artist`, `duration`)"
. "VALUES ('$title', '$artist', '$duration')";
$result = mysql_query($sql);
Risking downvotes, but I can't comment at my level so in order to try help I'll assume the question is "how can I see the error" and try answer that, as there's not much else to go on;
First, is $con created successfully?
$con = new mysqli("sql server hostname or ip", "user", "password", "schema/db name");
if($con->connect_errno > 0)
{
die('Unable to connect to database [' . $con->connect_error . ']');
}
As per comments, problem solved due to ' vs ` for column names.
$sql = "INSERT INTO test_xml (`title`, `artist`, `duration`) VALUES ('$title', '$artist', '$duration')";
if(!$result = $con->query($sql)) {
die('There was an error running the query [' . $con->error . ']');
} else {
echo "Successful query.";
}
this portion was really to include the error handling to see what the error was.
ok it was in the line:
$sql = "INSERT INTO test_xml ('title', 'artist', 'duration')
in the columms,
the ' must be ` like #Mihai and #RiggsFolly said.
I have a form that sends data to the php below. No errors appear but no information is inserted into the database and I don't understand why. I have triple checked all the table names etc and everything is correct. The code echo's out what I put into the form but it doesn't update to the database!
<?php
//variables for db
$username = "";
$password = "";
$hostname = "localhost";
$dbname = "infinity";
//connection to the database
$con = mysql_connect($hostname, $username, $password);
if($con == FALSE)
{
echo 'Cannot connect to database' . mysql_error();
}
mysql_select_db($dbname, $con);
$name=$_POST["name"];
$logo=$_POST["logo"];
$logo="<img src=\"images/".$logo."\" alt=\"$name Logo\" />";
$blurb=$_POST["blurb"];
$link=$_POST["link"];
echo $name;
echo $logo;
echo $blurb;
echo $link;
//Insert Values into Database
mysql_query("INSERT INTO `infinity`.`sponsors` (`name`, `logo`, `blurb`, `link`) VALUES ('$name', '$logo', '$blurb', '$link');");
?>
Try to get an error message of you query:
mysql_query($your_query) OR die(mysql_error());
try this
mysql_query("INSERT INTO `infinity`.`sponsors` (`name`, `logo`, `blurb`, `link`)
VALUES ('$name', '$logo', '$blurb', '$link')") or die(mysql_error());
else you make check it.
$sql ="INSERT INTO `infinity`.`sponsors` (`name`, `logo`, `blurb`, `link`)
VALUES ('$name', '$logo', '$blurb', '$link')";
$sqlinset= mysql_query($sql) or die(mysql_error());
echo $sql;
echo $sqlinset;
Try this:
mysql_query("INSERT INTO `infinity`.`sponsors` (`name`, `logo`, `blurb`, `link`) VALUES ('$name', '$logo', '$blurb', '$link');", $con);
And be sure to secure those variables you put in your database from SQL ijection and XSS attacks.
Hi guys my process page does not work, my code is
<?php
$id = $_POST['item_id'];
$qty = $_POST['item_qty'];
$name = $_POST['item_name'];
$con = mysqli_connect ("localhost", "name", "password", "db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT INTO Temp (id, qty, name)
VALUES
('$_POST[id]', '$_POST[qty]', '$_POST[name]')";
if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_error());
}
header('Location: http://url.com/');
mysqli_close($con);
?>
Should be all correct, just copy from w3school,
The problem is, the db only get 0,
ie. my $id is 4, $qty is 12, $name is "Hello", after the process page, the table only get two 0s in id and qty, name is void.
The values should be processed to this process page successfully, bc I have tried
echo $id, $qty, $name;
All are the same as I typed in before.
Could anyone help me? thanks :-)
this line:
INSERT INTO Temp (id, qty, name) VALUES ('$_POST[id]', '$_POST[qty]', '$_POST[name]')";
should be:
INSERT INTO Temp (id, qty, name) VALUES ('$id', '$qty', '$name')";
If the form is from your previous question, you dont need:
$id = $_POST['item_id'];
$qty = $_POST['item_qty'];
$name = $_POST['item_name'];
I agree it looks like you left out item_. You might want to sanitize your data first.
$id=mysqli_real_escape_string($_POST['item_id']);
$qty=mysqli_real_escape_string($_POST['item_qty']);
$name=mysqli_real_escape_string($_POST['item_name']);
$sql = "INSERT INTO Temp (id, qty, name)
VALUES ('$id', '$qty', '$name')";
I have a table with patients info and another one for their telephone numbers.
The relationship between them, is 1:N (1 to many). I put one restriction: I want all patients have at least one telephone number. So, I put a transaction in my code like this.
// Beggining of transaction
mysqli_query($dbc,'begin');
$sql1 = " INSERT INTO `dental_clinic`.`patient` (`idpatient` ,`surname` ,`name` ,`middle_name` ,`address` , `town`, `birth_date` , `occupation` , `email` ,`p_comments`) VALUES (NULL , '$surname', '$name', '$middle_name', '$address', '$town', '$birth_date', '$occupation', '$email', '$other');
";
$execute_sql1 = mysqli_query($dbc, $sql1);
$last_id = mysqli_insert_id($dbc);
// Put some variables to the insert queries of telephone.
$sql2_1 = " INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone') ";
$sql2_2 = "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone2')";
$sql2_3 = "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone3')";
// Checking the first field of telephone
if (!empty($phone)){
$execute_sql2_1 = mysqli_query($dbc, $sql2_1);
} else {
$execute_sql2_1 = false;
}
// Checking the second field of telephone
if (!empty($phone2)){
$execute_sql2_2 = mysqli_query($dbc, $sql2_2);
} else {
$execute_sql2_2 = true;
}
// Checking the third field of telephone
if (!empty($phone3)){
$execute_sql2_3 = mysqli_query($dbc, $sql2_3);
} else {
$execute_sql2_3 = true;
}
// Checking the insert commands to execute toggether
if ($execute_sql1 && $execute_sql2_1){
if ($execute_sql2_1 && $execute_sql2_2){
if ($execute_sql2_1 && $execute_sql2_3){
mysqli_query($dbc, 'commit');
echo 'The patient personal details inserted succesfully!';
echo 'The primary telephone inserted succesfully! ';
header("Location: new_medical_history.php");
} else {
mysqli_query($dbc, 'rollback');
echo 'Error, on 3rd phone! ';
}
} else {
mysqli_query($dbc, 'rollback');
echo 'Error, on 2nd phone! ';
}
} else {
mysqli_query($dbc, 'rollback');
echo 'Error, on patient personal details or on primary telephone! ';
}
// Ending connection
mysqli_close($dbc);
Both tables, patient and telephone are in the same form.
When I add succesfully a new patient, the autocrement idpatient value, jumps by two (1,3,5,7 and so on.) But when I comment out the transaction,
// mysqli_query($dbc,'begin);
// mysqli_query($dbc, 'commit');
// mysqli_query($dbc, 'rollback');
then the autoincrement value goes normal by one (7,8,9,10, .. etc.).
Could you please tell why this happening? Is it something wrong in my code? I want to keep the transaction, so noone of patients will be added without the primary telephone number.
I find your logic hard to follow, so I have rewritten your code using a try-catch, I believe it satisfies the same requirements and is easier to follow. Does this still cause a jump-by-2?
// fixme: consider using prepared statements to avoid sql injection attacks
mysqli_query($dbc,'begin');
try {
if(!mysqli_query($dbc, "INSERT INTO `dental_clinic`.`patient` (`idpatient` ,`surname` ,`name` ,`middle_name` ,`address` , `town`, `birth_date` , `occupation` , `email` ,`p_comments`) VALUES (NULL, '$surname', '$name', '$middle_name', '$address', '$town', '$birth_date', '$occupation', '$email', '$other')")) throw new Exception('Error, on patient personal details!');
$last_id = mysqli_insert_id($dbc);
if(empty($phone)) throw new Exception('Error, primary phone required!');
if(!empty($phone) && !mysqli_query($dbc, "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone')")) throw new Exception('Error, on primary telephone!');
if(!empty($phone2) && !mysqli_query($dbc, "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone2')")) throw new Exception('Error, on 2nd phone!');
if(!empty($phone3) && !mysqli_query($dbc, "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone3')")) throw new Exception('Error, on 3rd phone!');
mysqli_query($dbc, 'commit');
// these will not be seen if you immediately redirect
//echo 'The patient personal details inserted succesfully!';
//echo 'The primary telephone inserted succesfully! ';
header("Location: new_medical_history.php");
} catch(Exception $e) {
mysqli_query($dbc, 'rollback');
echo $e->getMessage();
}
// Ending connection
mysqli_close($dbc);
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("d/m/y : H:i:s", time());
$dbc = mysqli_connect('localhost', 'root', 'derp', 'derpdb')
or die("Database connection fried.");
$query = "INSERT INTO ipstore (tstamp, ip), " .
"VALUES ('$date', '$ip')";
mysqli_query($dbc, $query);
mysqli_close($dbc);
?>
Can anyone tell me what's wrong with this code? It's meant to store the users IP/date they requested the page in the database. I've tried replacing localhost with 127.0.0.1, no luck. It doesn't bring a message, so it must be connected, however when it comes to querying it just doesn't do it. And it doesn't give a warning. I've checked the DB, nothings there.
Also don't worry, nothing sensitive is there ;)
Thanks
$query = "INSERT INTO ipstore (tstamp, ip), " . "VALUES ('$date', '$ip')";
You are not supposed to use a comma after specifying columns - try
$query = "INSERT INTO ipstore (tstamp, ip) VALUES ('$date', '$ip')";
try it this way
$query = mysql_query("INSERT INTO ipstore (tstamp,ip) VALUES ('$date', '$ip')") or die(mysql_error()); if($query) {echo 'Success'; esle { echo 'Failed'; }
And you will get success for sure