How to debug query regarding $_FILES especially file inputs? - php

I had this query using php in inserting images after submitting the form It says "Requirements submitted succesfully" but there is no data inserted in database.
This is my code so far:
if(isset($_POST['sumit'])){
$count = count($_FILES);
$query = "SELECT * FROM dummyclients_tbl WHERE user_id = '".$_SESSION['user']."'";
if (!$result = mysql_query($query)) {
exit(mysql_error());
}
if(mysql_num_rows($result)){
$row = mysql_fetch_assoc($result);
$sid = $row['user_id'];
$coll =$row['college'];
$stat = "Pending";
$query = "INSERT INTO request_tbl (user_id,document_id,imgreq1,imgreq2,imgreq3,imgreq4,imgreq5,imgreq6,imgreq7,request_status,college) VALUES ('$sid','$passed_id'";
for($i = 1; $i <= $count; ++$i){
if(is_uploaded_file($_FILES['imgreq'.$i]['tmp_name']) && $_FILES['imgreq'.$i]['size']){
$query .= ",'" . base64_encode(file_get_contents(addslashes($_FILES['imgreq'.$i]['tmp_name']))) . "'";
}else{
$query .= ",NULL";
}
}
$query .= ",'$stat','$coll')";
?>
<script>alert('Requirements Successfully Submitted!');</script>
<?php
// saveimage($query);
}
else{
?>
<script>alert('Error while submitting form!');</script>
<?php
}
}
I dont know where did I go wrong so please if anyone can help I appreciate it. Thanks.

So it is true that I did not execute the query and forgot to put mysql_query($query); after $query .= ",'$stat','$coll')"; . And that lead me to solving another problem wherein I did not set the fields in the database to receive NULL values which is the cause of the error.

after:
$query .= ",'$stat','$coll')";
add
mysql_query($query)

Related

Why MYSQLi does not update the DB record, but it does provide a successful message

Why MYSQLi does not update the DB record, but it does provide a successful message. Of course, with the following message: 0 records UPDATED successfully And no changes are made to the database.
my index php file code:
<?php
include 'connect.php';
$work = $_GET["work"];
if($work == "select"){
$query = "SELECT * FROM login ORDER BY City DESC";
$result = $connect->prepare($query);
$result ->execute();
$out = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$record = array();
$record["InsID"] = $row["InsID"];
$record["Password"] = $row["Password"];
$record["Name"] = $row["Name"];
$record["City"] = $row["City"];
array_push($out,$record);
}
echo json_encode($out);
} elseif($work == "update"){
$name2 = $_REQUEST["Ali"];
$code2 = $_REQUEST["4779"];
$city2 = $_REQUEST["teh"];
$pass2 = $_REQUEST["123"];
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2'";
$result2 = $connect->prepare($query2);
$result2 ->execute();
}
?>
I really do not know where my coding is wrong. Please help.
I don't get why you are updating InsID and also using 'where InsID like'
Also there is additional ; in query
You may try
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID like '$code2'";
Important = sanitize input data first**
if I understand what you're trying to accomplish then :
you don't have to set InsID again
you need to use = and not LIKE in the WHERE condition
i.e. this is the row you need :
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2';";
also see Nico Haase's comment, it's super correct ! you must improve the code security, see : http://php.net/manual/en/security.database.sql-injection.php
Try this code
May be useful
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2';
if(mysqli_affected_rows($connect)==1){
echo "updated successfully";
}
else{
echo "failed";
}

Checking the SUM of specific column with condition before inserting data

I need help! I just want to insert data from user but before that it will check first if the 'dami' column in database is <=100, if yes then it will insert the data entered by the user if no then it will prompt the user.
But with my code it keeps inserting regardless of the 'dami' sum. Badly need it! Here's my code!
public function do_create_businesscard($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces,$is_deleted)
{
$sql = "SELECT (SELECT SUM(dami)
FROM tbl_business_card_information
WHERE is_deleted = 1)
AS 'Total'";
if ('Total' >= 100){
echo "Sorry but you already reach you maximum request";
}else{
$sql = "INSERT INTO tbl_business_card_information (`tel_no`,`tel_no1`,`fax_no`,`mobile_no`,`mobile_no1`,`email`,`email1`,`dami`) VALUES (?,?,?,?,?,?,?,?)";
$data = array($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces);
$query = $this->db->query($sql,$data);
return $query;
}
}
Thank You !
Your usage of SELECT is wrong. Here is the way.
public function do_create_businesscard($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces,$is_deleted)
{
$sql = "SELECT SUM(dami) FROM tbl_business_card_information WHERE is_deleted = 1";
$result = $this->db->query($sql);
$row = $result->fetch_array(MYSQLI_NUM);
$total = $row[0];
if ($total > 100){
echo "Sorry but you already reach you maximum request";
}else{
$sql = "INSERT INTO tbl_business_card_information (`tel_no`,`tel_no1`,`fax_no`,`mobile_no`,`mobile_no1`,`email`,`email1`,`dami`) VALUES (?,?,?,?,?,?,?,?)";
$data = array($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces);
$query = $this->db->query($sql,$data);
return $query;
}
}

PHP insert statement is only inserting 1 time

im using php to insert into the review table.ive given the variables $email, $starcount, $bookid fixed values for now just to test the file. the $res query checks to see if there is a row with that book id and email in it. if theres not The $sql query inserts it, and then the $nex query loops through taking any starcount columns where the book column = $book.
if i change the the email at the top of the file it should insert into the new info database and pull out the new and existing starcount, but it does not post, it just returns the already existing starcount. i dont understand why its not working .... im using the array to return to my file.
<?php
mysql_connect("localhost","root","");
mysql_select_db("FunReads");
$email = "sd";
$starcount = "2";
$bookid = "5";
$res = mysql_query("SELECT * FROM Review WHERE book_id='$bookid' AND user_email='$email'");
if (mysql_num_rows($res) != 0) {
$array[]= array("starcount" => "already entered");
} else {
$sql = mysql_query("INSERT INTO Review(book_id,starcount,user_email) values('.$bookid.','.$starcount.','.$email')");
$nex = mysql_query("SELECT * FROM Review WHERE book_id='$bookid'");
while($row = mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
//echo "[{"name":"n1","city":"NY"},{"name":"n2","city":"Paris"}, ...]
?>
It seems to me "book_id" in "Review" table is primary key, as you have tried to add it multiple time, system shows the error duplicate key. Check the error & post it. Also check whether insert query is working or not.
you should not pass the primary key value manually
try this it will helps you
<?php
mysql_connect("localhost","root","");
mysql_select_db("FunReads");
$starcount="2";
$email = "vinodh#gmail.com";
$res=mysql_query("SELECT * FROM Review WHERE email ='$email'");
if(mysql_num_rows($res)!=0){
$array[]= array("starcount" => "already entered");
}else{
$sql=mysql_query("INSERT INTO Review (starcount,email) values('.$starcount.','.$email')");
$nex=mysql_query("SELECT * FROM Review WHERE email='$email'");
while($row=mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
?>
I just updated your code and it is working fine for me.
<?php
mysql_connect("localhost","user","");
mysql_select_db("xyz");
$email = "hari#gmail.com";
$starcount = "2";
$bookid = "5";
$sql = "SELECT * FROM review WHERE book_id='$bookid' AND user_email='$email'";
$res = mysql_query($sql);
if (mysql_num_rows($res) != 0) {
$array[]= array("starcount" => "already entered");
} else {
$sql = "INSERT INTO review(book_id,starcount,user_email) values('$bookid','$starcount','$email')";
$sql = mysql_query($sql);
$nex = mysql_query("SELECT * FROM review WHERE book_id='$bookid'");
while($row = mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
sample output :
[{"starcount":"2"},{"starcount":"3"},{"starcount":"1"},{"starcount":"2"},{"starcount":"1"}]
I updated the insert query, please try to update the same and test.

PHP script, getting a pending request list

I have only just started writing the PHP script to power the backend of my android app. What i'm currently trying to do is run a PHP script that goes into my database called send_friendreq and the table called pending_req and gets the row toUser and adds it to an array. The only problem i'm currently having is the fact that I cannot get the PHP script to run correctly. Any help would be appreciated. Here is my code for the PHP script that I currently have. Thank you very much for the help!
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql="SELECT fromUser FROM pending_req WHERE toUser='&username'";
$result = mysqli_query($con, $sql);
$array = array();
while ($row = pg_fetch_array($result))
{
$i++;
}
for($x=0;$x<$i;$x++)
{
echo $array[$x];
}
if(!$result)
{
echo 'Failed';
}
else
{
echo json_encode($array[$x]);
echo "<br>";
}
If you have suggestions on something that would work better / more efficiently / safer, please let me know!
If I am not wrong, you are trying to get json of results from your query. try this code.
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql = 'SELECT fromUser FROM pending_req WHERE toUser='. $username ;
$result = mysqli_query($con, $sql);
if(!$result) {
echo 'Failed';
}elseif($result){
$myArray = array();
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
you query syntax seems to be not correct plz modify as :
$sql="SELECT fromUser FROM pending_req WHERE toUser='$username'";

DELETE WHERE AND not functioning as expected

I am attempting to delete rows from a databases. The DELETE query is checking for a match in date from an array and that reason is empty. My code is;
<?php
session_start();
include 'connection.php';
include 'DateTest.php';
$deleted = array();
$size = sizeof($dayOfTheWeek) - 1;
for ($count = 0; $count <= $size; $count++) {
$query = "DELETE FROM daysoff WHERE DATE(start) = '$dateMonthYearArr[$count]' AND reason = ''";
mysql_query($query) or die("im dead1");
$deleted[] = "Rota deleted for ". $dateMonthYearArr[$count] .".</br>";
} $_SESSION['delete'] = $deleted;
header('Location: '. $_SERVER['HTTP_REFERER']) ;
?>
This DELETE query is dying, if I remove one of the WHERE arguments, either one, it works, but the AND seems to be an issue. Where is this failing?
Try
$query = "DELETE FROM daysoff WHERE DATE(start) = '" . $dateMonthYearArr[$count] . "' AND reason = ''";
And see how it goes...

Categories