I'm trying to query the data base to check whether the marks for current schedule is already updated in the data base before inserting, My code as follows:
<?php
include 'config.php';
if(isset($_POST['submit'])){
$schedule = $_POST['hischedule'];
$array = $_POST['marks'];
//Qry to check Duplicate values Againest Database
$marksupdqry = "select distinct scheduleName from marks";
$mks = mysqli_query($link, $marksupdqry);
while($mksresult = mysqli_fetch_array($mks))
foreach($mksresult as $sdlnme)
if($sdlnme = $schedule){
echo "<script> alert('Marks for this schedule already exist in db');</script>";
echo "<script> window.location.href='marks.php';</script>";
}else{
foreach ($array as $reg_num=>$score)
{
$ins = "INSERT INTO marks (sl_no, scheduleName, obtainedMarks) VALUES ('$reg_num', '$schedule', '$score')";
$res = mysqli_query($link, $ins);
if($res){
//echo "Success";
$Message = urlencode("Marks updated successfully ");
header("Location:marks.php?Message=".$Message);
die;
}
}
}
}
?>
My problem is I'm unable to execute both if and else part of my code.
Modify your code like bellow:
<?php
include 'config.php';
if(isset($_POST['submit'])){
$schedule = $_POST['hischedule'];
$array = $_POST['marks'];
$marksupdqry = "select distinct scheduleName from marks";
$mks = mysqli_query($link, $marksupdqry);
while($mksresult = mysqli_fetch_array($mks)){
foreach($mksresult as $sdlnme){
if($sdlnme == $schedule){
echo "<script> alert('Marks for this schedule already exist in db');</script>";
echo "<script> window.location.href='marks.php';</script>";
}else{
foreach ($array as $reg_num=>$score)
{
$ins = "INSERT INTO marks (sl_no, scheduleName, obtainedMarks) VALUES ('$reg_num', '$schedule', '$score')";
$res = mysqli_query($link, $ins);
if($res){
//echo "Success";
$Message = urlencode("Marks updated successfully ");
header("Location:marks.php?Message=".$Message);
die;
}
}
}
}
}
}
?>
Related
I would like to see where my code is incorrect. I want to store values from my database as a php array. Then I'd like to store the individual parts of the array as separate variables. Here is my code:
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
$comment0 = $comments[0];
$comment1 = $comments[1];
$comment2 = $comments[2];
$comment3 = $comments[3];
$comment4 = $comments[4];
$comment5 = $comments[5];
$comment6 = $comments[6];
$comment7 = $comments[7];
$comment8 = $comments[8];
$comment9 = $comments[9];
?>
This will run your mysql query and add each comment to an array of comments, then print the array.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = array();
while($comment = mysqli_fetch_row($result)){
$comments[] = $comment;
}
print_r($comments);
?>
not sure if you are in console or through web server.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
foreach($comments as $comment){
echo print_r($comment,1).'--------------\r\n<br>\r\n';
}
?>
this is called a loop. loop is your friend.
I'm a learner of PHP. This commit and rollback process does the task at each line. How can I commit and rollback all at once?
<?php
$con = mysql_connect('localhost','user', 'abcdefg');
if(!con)
{
echo "Can't connect to db.";
}else {
mysql_select_db('test');
}
if(!file_exits('test.csv')
{
echo "Can't find the file.";
}
$ar_1 = file('test.csv', FILE_IGNORE_NEW_LINES);
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === true)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}else {
//Rollback
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
}
}
mysql_close($con);
?>
you need to commit out of the loop, i used $bool to see if there somewhere the $result isn't true
it will look something like :
edit:
breaking from the loop when something is wrong as you don't need to continue looping to other elements.
$bool = 1;
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === false)
{
//Rollback
$bool = 0;
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
break;
}
}
if($bool == 1)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}
mysql_close($con);
I want edit record in db table but it doesn't save in db table and nothing changed after i submit this form.
Here codes that i forgot to put.
<?php
require('db.php');
include("auth.php"); //include auth.php file on all secure pages
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
This is my php codes
<?php
if(isset($_POST['new']) && $_POST['new']==1)
{
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
$title =$_REQUEST['title'];
$date = $_REQUEST['date'];
$from_to = $_REQUEST['from_to'];
$details = $_REQUEST['details'];
$d_location = $_REQUEST['d_location'];
$d_stat = $_REQUEST['d_stat'];
$update="update doc set title='".$title."', date='".$date."', from_to='".$from_to."', details='".$details."', d_location='".$d_location."', d_stat='".$d_stat."' where id_doc='".$id_doc."'";
mysqli_query($connection, $update) or die(mysql_error());
$status = "File Record Updated Successfully. </br></br><a href='v_doc.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
// here some else code
}
?>
Not an answer. Too long for a comment.
The issue of parametrised queries aside, I find this easier to read:
UPDATE doc
SET title = '$title'
, date = '$date'
, from_to = '$from_to'
, details = '$details'
, d_location = '$d_location'
, d_stat = '$d_stat'
WHERE id_doc = '$id_doc'
And now see about parametrised queries
Try below:
<?php
if(isset($_POST['new']) && $_POST['new']==1)
{
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
$title =$_REQUEST['title'];
$date = $_REQUEST['date'];
$from_to = $_REQUEST['from_to'];
$details = $_REQUEST['details'];
$d_location = $_REQUEST['d_location'];
$d_stat = $_REQUEST['d_stat'];
$update="update doc set title='".$title."', date='".$date."', from_to='".$from_to."', details='".$details."', d_location='".$d_location."', d_stat='".$d_stat."' where id_doc='".$id_doc."'";
if(mysqli_query($connection, $update)) {
$status = "File Record Updated Successfully. </br></br><a href='v_doc.php'>View Updated Record</a>";
} else {
die(mysqli_error($connection));
}
echo '<p style="color:#FF0000;">'.$status.'</p>';
} else {
// here some else code
}
?>
This should show you exact error, once you get it. show it here, so we can check and do correction.
So I have a database and I have written a php file that inserts value to the table and then returns all the values in this table.
However, my code only returns just one random value from the table and not all of them. I am not sure why, but this is my code:
<?php
include_once "init.php";
if(!empty($_POST['names'])){
$contactname = $_POST['names'];
$query = "INSERT INTO contacts (contactID, names)
VALUES('NULL', ?)";
$results = $conn -> prepare($query);
$results->bind_param('s', $contactname);
$results->execute();
$results->close();
echo json_encode("Success");
$query_two = "SELECT names FROM contacts";
$result = mysqli_query($conn, $query_two);
$response = array();
if(mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){
$response["names"][] = $row["names"];
}
}
echo json_encode($response);
}
else{
echo json_encode("Something went wrong");
}
?>
EDIT: Thank you guys for providing me a solution so quickly! I fixed it and it works but the first echo json_encode("Success"); is not being executed.
In your line here:
$response["names"] = $row["names"];
You are replacing the value of $response["names"]. Instead, try adding it to an array:
$response = array("names" => array());
if(mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){
$response["names"][] = $row["names"];
}
}
To use one JSON object, you'd initialize $response at the top and change the values as needed.
<?php
include_once "init.php";
$response = ['success' => false, 'message' => null, 'names' => []];
if(empty($_POST['names'])) {
$response['message'] = 'No names were provided in the request';
} else {
$contactname = $_POST['names'];
$query = "INSERT INTO contacts (contactID, names) VALUES('NULL', ?)";
$results = $conn->prepare($query);
$results->bind_param('s', $contactname);
$results->execute();
$results->close();
$response['success'] = true;
$query_two = "SELECT names FROM contacts";
$result = mysqli_query($conn, $query_two);
if(mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){
$response['names'][] = $row['names'];
}
}
}
echo json_encode($response);
I'm trying to fetch couple of single data in my server database but this is throwing some errors. The incoming data is correct. The search function just don't get completed.
Here's the code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxx');
define('PASS','xxxxxxxxx');
define('DB','xxxxxxxxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
$post_id = $_POST['id'];
$buyer_mobile = $_POST['mobile'];
$buyer_name = $_POST['name'];
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysqli_query($con,$sql);
$owner_mobile = $row['mobile'];
$sql = "select name from user where mobile='$owner_mobile'";
$r = mysqli_query($con,$sql);
$owner_name = $row['name'];
$sql = "INSERT INTO flat_booking (post_id,owner_mobile,owner_name,buyer_mobile,buyer_name) VALUES ('$post_id','$owner_mobile','$owner_name','$buyer_mobile','$buyer_name')";
if(mysqli_query($con,$sql)){
echo "Success";
}
else{
echo "error";
}
mysqli_close($con);
}else{
echo 'error1';
}
What am I doing wrong here? Maybe this:
$owner_mobile = $row['mobile'];
Thanks in advance!
create table flatower and add mobile column
$post_id = 1;
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysql_query($con,$sql);
$row = mysql_fetch_array($res);
$owner_mobile = $row[0]['mobile'];
Your problem is this line:
$owner_mobile = $row['mobile'];
You have not created the $row variable. For this you would need to do something such as:
Do this first:
<?php
$row = array();
while ($result = mysqli_fetch_assoc($res))
{
$row[] = $result;
}
?>
This allows you to do this:
<?php
foreach ($row as $r)
{
var_dump($r); print "<br />"; // One row from the DB per var dump
}
?>