Fatal error Uncaught mysqli_sql_exception you have an error with your sql syntax (line 25) [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
im getting this weird error and i don't know what im doing wrong
it has something to do with this line:
$result = mysqli_query($connection,$query);
plex help me fix this
this is my code:
<?php
define("HOSTNAME", "localhost");
define("USERNAME", "root");
define("PASSWORD", "");
define("DATABASE", "courses");
$connection = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE);
if(isset($_POST['add_data'])) {
$name = $_POST['name'];
$description = $_POST['description'];
$duration = $_POST['duration'];
$price = $_POST['price'];
if($name == "" || empty($name)) {
header('location:index.php?message= you need to fill in your name');
} else {
$query = "insert into `courses` (`name,` `description`, `duration`, `price`) values
('$name', '$description', '$duration', '$price')";
$result = mysqli_query($connection,$query); ERROR line 25
if(!$result) {
die("query Failed".mysqli_error());
} else {
header('location:index.php?insert_msg= your data has been added');
}
}
}
?>
my index page:
https://pastebin.com/diNNpPWk
im trying to make a crud application but i'm unable to insert any data

Related

Assign value from $data['id']; [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to assign the value of $data['id'] to a variable like this:
$totalfor = $data['id'];
but this is not working, the value is not passing to $totalfor
If i echo $data['id'], it gives the right value which is 85
i need the value of $data['id'] to use it into a Select query
Anyone know the correct syntax to achieve this ?
Thanks
Here is the code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$jury = get_active_user('accountname');
$totalfor = $data['id'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT total AS total FROM votestepone WHERE votefor = '$totalfor' AND votedby= '$jury'";
$result = $conn->query($sql);
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$totalvote = $row["total"];
}
} else {
echo "0 results";
}
?>
<?php echo $totalvote; ?>
The error is : Undefined variable: totalvote
If i set
$totalfor = 85; --> it works
but i need to use the value coming from $data['id'];
I'm in a view page and $data['id'] is coming from:
$data = $this->view_data;
if I <?php echo $data['id']; ?> it show 85
mysqli_query returns false when there is an error not if there are no results. The error says $totalvote is undefined, not $totalfor so the query didn't find a single result.
var_dump($sql) and make sure the query is correct and it really does fetch a result row when run against your database.

I am getting syntax error, unexpected 'header' (T_STRING) in http://localhost:80/opt/lampp/htdocs/user_control.php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying for user login and registration forms in Android. From past 5 days, I am trying it. I am not able to get it. Can anyone help me please? This is my code user_control.php. I am including the connection.php in user_control.php.
<? php
class user {
private $db;
private $connection;
/* The below one is the consttructor*/
function __construct() {
header('Content-Type: application/json'); /* used for including json format*/
require_once 'connection.php';
$this->db = new DB_Connection();
$this->connection = $this->db->get_connection();
}
public function does_user_exist($email,$password) {
$query = "Select * from userss where email ='$email' and password = '$password'";
$result = mysqli_query($this->connection,$query);
if(mysqli_num_rows($result) > 0){
$json['success'] = 'welcome'.$email;
echo json_encode($json);
mysqli_close($this->connection);
} else {
$query = " Insert into userss(email,password) values ('$email','$password')";
$is_inserted = mysqli_query($this->connection, $query);
if($is_inserted == 1) {
$json['success'] = 'Account created, welcome '.$email;
} else {
$json['error'] = 'Wrong password';
}
echo json_encode($json);
mysqli_close($this->connection);
}
}
}
$user = new user();
if(isset($_POST['email'],$_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
if(!empty($email) && !empty($password)) {
$encrypted_password = md5($password);
$user -> does_user_exist($email,$encrypted_password);
} else {
echo json_encode("You must fill both fields");
}
}
While I am running with postman software by Chrome I am getting the error as I am getting syntax error:
unexpected 'header' (T_STRING) in http://localhost:80/opt/lampp/htdocs/user_control.php
Remove the space in your line 1. Should looks like:
<?php
Try to comment:
//header('Content-Type: application/json'); /* used for including json format*/
Source: https://stackoverflow.com/a/20620606/2381906

Closing connection in order to get remaining queries to run [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Perhaps I'm missing something very obvious here.
My code doesn't seem to work below the 'GET BOOK ID' annotation if I don't close and reopen the connection.
While it does work, it doesn't seem to be an efficient way at all to do this, any suggestions?
$AddBook = $conn->prepare("CALL makeBooking((SELECT CustID FROM customer WHERE UserName = '$UserName'), ?, ?, ?, ?, now())");
$AddBook->bind_param('iiis',$PerfID, $NumAdults, $NumChilds, $ColTicket);
if ($AddBook->execute())
{
//----------------DEDUCT SEATS--------------//
$SeatDeduction = $conn->prepare("CALL deductSeats($TotalSeats,?)");
$SeatDeduction->bind_param('i',$PerfID);
$SeatDeduction->execute();
mysqli_close($conn);
require ('connect.php');
//-----------------GET BOOK ID--------------//
$getBookID = "CALL getBookByUserName('$UserName')";
$result2 = mysqli_query($conn, $getBookID);
$Output2 = mysqli_fetch_assoc($result2);
$BookID = $Output2['BookID'];
mysqli_close($conn);
require ('connect.php');
//-------------------SET COST---------------//
$setCost = "CALL setBookingPrice($BookID)";
mysqli_query($conn,$setCost);
mysqli_close($conn);
require ('connect.php');
//-------------------GET COST---------------//
$getCost = "CALL getCost($BookID)";
$result6 = mysqli_query($conn,$getCost);
$Output6 = mysqli_fetch_assoc($result6);
$Cost = $Output6['TotalCost'];
mysqli_close($conn);
require ('connect.php');
//---------------BOOKING CONFIRM------------//
$ShowRef = 'Booking Completed<p>Reference Number: <b>' . $BookID . '</b><p>';
$showCost = 'Total Cost: <b><u>£' . $Cost . '<u><b><br>';
$Confirm = $ShowRef.$showCost;
}
else
{
die(mysqli_error($conn));
}
mysqli_close($conn);
You don't have to keep including the connect.php file over and over.
Assuming you already included it somewhere at the top since you run $AddBook->execute()
See this updated code:
$AddBook = $conn->prepare("CALL makeBooking((SELECT CustID FROM customer WHERE UserName = '$UserName'), ?, ?, ?, ?, now())");
$AddBook->bind_param('iiis',$PerfID, $NumAdults, $NumChilds, $ColTicket);
if ($AddBook->execute())
{
//----------------DEDUCT SEATS--------------//
$SeatDeduction = $conn->prepare("CALL deductSeats($TotalSeats,?)");
$SeatDeduction->bind_param('i',$PerfID);
$SeatDeduction->execute();
$SeatDeduction->close();
$conn->next_result();
// mysqli_close($conn); // remove this
// require ('connect.php'); // remove this
//-----------------GET BOOK ID--------------//
$getBookID = "CALL getBookByUserName('$UserName')";
$result2 = mysqli_query($conn, $getBookID);
$Output2 = mysqli_fetch_assoc($result2);
$BookID = $Output2['BookID'];
// mysqli_close($conn); remove this
// require ('connect.php'); remove this
//-------------------SET COST---------------//
// ...
//-------------------GET COST---------------//
// ...
//---------------BOOKING CONFIRM------------//
// ...
}
else
{
die(mysqli_error($conn));
}
mysqli_close($conn);

error in mysql query string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
<form action = "index.php" method = "post">
username : <input type = "text" name = "uname" /><br>
password : <input type = "text" name = "pass" /><br>
submit : <input type = "submit" name = "submit" value = "submit" />
</form>
<?php
if(isset($_SESSION['id'])){echo $_SESSION['id'];}
if(isset($_POST['submit'])){
if ($_POST['submit'] == 'submit'){
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$db = "davidedwardcakes";
$connect = mysql_connect('localhost', 'root', 'wtfiwwu');
$db_connect = mysql_selectdb($db, $connect);
if(!$db_connect){echo 'no';}
$query = "SELECT * FROM `users` WHERE uname ='$uname' AND pass = '$pass'";
$result = mysql_query($query, $connect);
if(mysql_num_rows($result) > 0){//echo 'index failed'; var_dump($result);}
while($row = mysql_fetch_array($result)){echo $row['uname']
. "<br>";
session_start();
echo 'peruse';
$_SESSION['id'] = $row['id'];}}
else{echo 'lol'; var_dump($query);}}
Whenever I want to login, i get the error:
string 'SELECT * FROM users WHERE uname ='brown' AND pass = 'kenji'' (length=61)
meaning that theres a problem with my $query. If I remove the $pass query from $query it works fine but doesn't when it is included. Can anybody help please.
Let me convert your code to MySQLi at least. MySQL is already deprecated.
<?php
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase"); /* REPLACE NECESSARY DATA */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
/* REPLACE THE NECESSARY POST DATA BELOW AND PRACTICE ESCAPING STRINGS BEFORE USING IT INTO A QUERY TO AVOID SOME SQL INJECTIONS */
$uname=mysqli_real_escape_string($connect,$_POST['username']);
$pass=mysqli_real_escape_string($connect,$_POST['password']);
$query = "SELECT * FROM `users` WHERE uname ='$uname' AND pass ='$pass'";
$result = mysqli_query($connect,$query); /* EXECUTE QUERY */
if(mysqli_num_rows($result)==0){
echo 'login failed';
var_dump($result);
}
else {
while($row = mysqli_fetch_array($result)){
echo $row['uname'];
} /* END OF WHILE LOOP */
echo 'Successfully Logged-in.';
var_dump($query);
} /* END OF ELSE */
?>

Parse error: syntax error, unexpected '}' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got this error
Parse error: syntax error, unexpected '}' in C:\wamp\www\widget_corp\public
\create_subject.php on line 24
when I clicked on my "Creae Subject" button.
I have tried to fix it all day but really can't find what the problem is.
<?php require_once ("../includes/session.php"); ?>
<?php require_once ("../includes/db_connection.php"); ?>
<?php require_once ("../includes/functions.php"); ?>
<?php require_once ("../includes/validation_function.php"); ?>
<?php
if (isset($_POST['submit'])) {
// Process the form
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"];
// Validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);
$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);
if (!empty($errors)) {
$_SESSION["errors"] = $errors;
redirect_to("new_subject.php")
} <-------- THIS IS ROW 24! ----------->
$query = "INSERT INTO subjects (";
$query .= " menu_name, position, visible";
$query .= ") VALUES (";
$query .= " '{$menu_name}, {$position}, {$visible}";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["message"] = "Subject created!";
redirect_to("manage_content.php");
} else {
// Failure
$_SESSION["message"] = "Subject creation failed";
redirect_to("new_subject.php");
}
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
?>
<!-- Close database connection -->
<?php if (isset($connection)) { mysqli_close($connection); } ?>
Missing semicolon on the line above the brace.
redirect_to("new_subject.php")
A lot of times you have to look at the line above the one given in the error to see what the problem is.
you have missed semicolon in this line:
redirect_to("new_subject.php");
try this:
if (!empty($errors)) {
$_SESSION["errors"] = $errors;
redirect_to("new_subject.php");
}

Categories