Jump to content

Recommended Posts

I am trying to convert my site to mysqli and I cannot get the databse to update or the results to show on the site.

 

If you could look at my code and please advise to what I could be doing wrong I would greatly appreciate it.

 

<?php


$db = new mysqli("localhost","admin","password","database"); 


if(!$db) {
    die('sorry we are having some problbems');
}


if ($_POST['submit']) {

$id = $_POST['id'];
$itemname = $_POST['itemname'];
$manufacture = $_POST['manufacture'];
$model = $_POST['model'];


//below are checkboxes
$showmanu = $_POST['showmanu'];
$showmodel = $_POST['showmodel'];
$showserial = $_POST['showserial'];


$query = "UPDATE new_equip SET itemname=?, manufacture=?, model=?, showmanu=?, showmodel=?, showserial=? WHERE id=?  LIMIT 1";
$conn = $db->prepare($query);
$conn->bind_param("sssiii", $item, $manufacture, $model, $showmanu, $showmodel, $showserial, $id);


if ($conn->execute()) {
header('location: inventory.php?Msg=Update');
}
$db->close();
}


?>

This is the tutorial and code I was using as reference.

http://coderlearner.com/PHP_MySQLi_Example_Update_Record
Link to comment
https://forums.phpfreaks.com/topic/295128-mysqli-update-not-working/
Share on other sites

I have the checkboxes so that if they are checked they show on the frontend. Thats a whole other issue I cant solve.

 

But it still didnt work even if I checked the box.

 

After doing some research, I seen that another way of doing it was like below. This where I am confused, Seems there are different ways to do this.

$query = "UPDATE new_equip SET `itemname`=?, `manufacture`=?, `model`=?, `showmanu`=?, `showmodel`=?, `showserial`=? WHERE `id`=? LIMIT 1";
$conn = $db->prepare($query);
$conn->bind_param('sssiii', $_POST['item'], $_POST['manufacture'], $_POST['model'], $_POST['showmanu'], $_POST['showmodel'], $_POST['showserial']);

It still isnt working. Here is what I have for the query.

 

$query = "UPDATE new_equip SET `itemname`=?, `manufacture`=?, `model`=?, `serial`=?, `year`=?, `condition`=?, `category`=?, `desc`=?, `dimension`=?, `location`=?, `price`=?, `purchase`=?, `addedby`=?, `notes`=?, `ran`=?, `electrical`=?, `owner`=?, `featured`=?, `showmanu`=?, `showmodel`=?, `showserial`=?, `showyear`=?, `showdem`=?, `showelec`=?, `showran`=?, `showloca`=?, `showown`=?, `showpur`=?, `showsale`=? WHERE id=?  LIMIT 1";
$conn = $db->prepare($query);
$conn->bind_param("sssssssssssssssssiiiiiiiiiiiii", $itemname, $manufacture, $model, $serial, $year, $condition, $category, $desc, $dimension, $location, $price, $purchase, $addedby, $notes, $ran, $electrical, $owner, $featured, $showmanu, $showmodel, $showserial, $showyear, $showdem, $showelec, $showran, $showloca, $showown, $showpur, $showsale, $EditID);

if ($conn->execute()) {
            header('location: inventory.php?Msg=Update');
        } 

$db->close();
}

I count 30 on both the placeholder and params

 
And when I click submit it is just showing a blank EditPost.php
Edited by laflair13

If you mean by adding this code to the .htaccess, it makes my site have a 500 error

 

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
 
So I created a php.ini file and added the code above to that. But still all I am getting is a blank EditPost.php page.
Edited by laflair13

That is at the top of edit-item.php. It grabs the item info from the database and pre-fills the fields on the page.

 

So here is what that file looks like.

 

edit-item.php (on the top)

<?PHP
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
    
  } else {
header ("Location: index.php");
}
 $eid = (int) $_GET['id'];

 include_once('../mysql_connect.php');
 if(isset($_POST['marksold']) && isset($_POST['id']))
 {
     mysql_query("UPDATE new_equip SET sold='1' WHERE id='" . mysql_real_escape_string($_POST['id']) . "'");
 }

?>

The form (Some fields removed to save room for post) 


<form method="post" action="EditPost2.php" enctype="multipart/form-data" class="form-horizontal" accept-charset="UTF-8">
    <div class="form-group">
        <label class="col-md-3">Item ID</label>
        <div class="col-md-8">
            <input type="text" name="EditID" value="<?php echo $row['id']; ?>" class="form-control" />
        </div> <!-- /.col -->
    </div> <!-- /.form-group -->
    
    <div class="form-group">
        <label class="col-md-3">Item Name</label>
        <div class="col-md-8">
            <input type="text" name="itemname" value="<?php echo $row['itemname']; ?>" class="form-control" />
        </div> <!-- /.col -->
    </div> <!-- /.form-group -->
    
    <div class="form-group">
        <label class="col-md-3">Manufacture</label>
        <div class="col-md-8">
            <input type="text" name="manufacture" value="<?php echo $row['manufacture']; ?>" class="form-control" />
        </div> <!-- /.col -->
        <input type="checkbox" name="showmanu" value="1" <?php echo ($row['showmanu'] == 1) ? 'checked="checked"' : ''; ?> />
        <span style="float:right; font-size: 10px; margin-top: 4px">Check to show</span>
    </div> <!-- /.form-group --><div class="form-group">
<div class="col-md-7 col-md-push-3">                                                    
<button type="submit" name="submit" class="btn btn-primary" >Save Changes</button>
       
<button type="reset" class="btn btn-default">Cancel</button>
</div> <!-- /.col -->
</div> <!-- /.form-group -->
 
                                                

 

Then of course my EditPost.php (Some fields taken out to save room)

<?php
error_reporting(E_ALL);

$db = new mysqli("localhost","admin","password","database"); 

if(!$db) {
    die('sorry we are having some problbems');
}

if(isset($_POST['submit'])) {

$id = $_POST['EditID'];
$itemname = $_POST['itemname'];
$manufacture = $_POST['manufacture'];

//Below are checkboxes
$showpur = $_POST['showpur'];
$showsale = $_POST['showsale'];

$query = "UPDATE new_equip SET `itemname`=?, `manufacture`=?, `showpur`=?, `showsale`=? WHERE id=?  LIMIT 1";
$conn = $db->prepare($query);
$conn->bind_param("ssiii", $itemname, $manufacture, $showpur, $showsale, $EditID);

if ($conn->execute()) {
            header('location: inventory.php?Msg=Update');
        } 
else echo $conn->error;


$db->close();
}

?>

 

Edited by laflair13

the error

Notice: Undefined index: id in /edit-item.php on line 9

is from edit-item.php. From what I see you try to set a value of $eid from a non-existent value but then it isn't used after that. The query uses a POST variable

I am trying to convert my site ...

 

then you would probably want to take the time to do it in general, so that you don't need to repeat code over and over.

 

you should extend whatever database library you are using (PDO works best for this), to create a generic prepared query method, that you can simply supply the sql statement and an (optional) array of input parameters/types, and it will handle preparing the query, binding any input data, running the query, and returning the result of the query (array of data, even if empty, for select/show queries, true/false status from delete/update queries.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.