-1

I'm just learning Mysql/PHP and I'm trying to update a mysql record from a selected value from the dropdown list. I have read through several tutorials, and tried to apply them, but I cannot get this working...

What I want : I got a dropdown list that get values from the mysql database( UNIQUE ID, Number, Model, Serialnumber, Capacity). I want to UPDATE the selected value from the dropdown menu. Every value from the dropdown menu got a UNIQUE ID.

Problem: When I hit submit, it does nothing. When I change the SQL query from WHERE id='$id'" TO WHERE id=23 it will update the record with id 23. So it has something to do with that.

I know my code is a mess, but will clean later. My code : ipad-uitlenen.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include("../includes/connect.php");
if(isset($_SESSION['logged_in'])){
  $sql="SELECT id, nr, model, serienummer, capaciteit FROM ipads WHERE uitgeleend='Nee' ORDER BY nr";
  $result1 = mysqli_query($db, $sql);
 
 if(isset($_POST['btnAdd'])) {
  $id=$_REQUEST['id'];
  $persoon=$_POST['persoon'];
  $datumuitgeleend=$_POST['datumuitgeleend'];
  $datumretour=$_POST['datumretour'];
  $opmerking=$_POST['opmerking'];
  $sql="UPDATE ipads SET uitgeleend='Ja', persoon='$persoon', datumuitgeleend='$datumuitgeleend', datumretour='$datumretour', opmerking='$opmerking' WHERE id='$id'";
  $result=$db->query($sql);
  header("location:overzicht-ipads.php");

 }
 include("../includes/get_header_wn.php");
?>

            <h1 class="page-title">iPad uitlenen</h1>
                    <ul class="breadcrumb">
            <li><a href="index.php">Home</a> </li>
            <li class="active">Nieuw</li>
        </ul>
        </div>

<form id="gegevensForm" class="col-xs-4" form method="POST" action="ipad-uitlenen.php">
 <div class="form-group">
 <select name="id">
    <?php while ($row1 = mysqli_fetch_array($result1)):;?>
    <option value="<?php echo $id?>"><?php echo $row1[1];?> / <?php echo $row1[3];?> / <?php echo $row1[2];?> / <?php echo $row1[4];?></option>
    <?php endwhile;?>
</select>
 </div>

 <div class="form-group">
        <label>Persoon</label>
        <input type="text" class="form-control" name="persoon" value="" />
    </div>
 
 <div class="form-group">
        <label>Datum uitgeleend</label>
        <input type="text" id="datepicker" class="form-control" name="datumuitgeleend" value="" />
    </div>
 
 <div class="form-group">
        <label>Datum retour</label>
        <input type="text" id="datepicker1" class="form-control" name="datumretour" value="" />
    </div>
 
 <div class="form-group">
  <label for="comment">Opmerking</label>
  <textarea class="form-control" rows="5" id="comment" name="opmerking"></textarea>
 </div>
 
    <button class="btn btn-primary pull-right" name="btnAdd" input type="submit"><i class="fa fa-save"></i> Opslaan</button>
    <a href="overzicht-ipads.php"><input type="button" name="btnCancel" value="Annuleer" class="btn btn-primary pull-left"></a>
    
    <?php
  include('../includes/get_footer.php');
 ?>
    
</form>

<?php
}
?>

Connect.php

<?php
 try {
  $db = mysqli_connect("localhost", "root", "", "i3a");
 }
 catch(PDOException $e){
  echo $e->getMessage();
  die();
 }
 //PDO(database:localhost:3307;dbnaam, root, wachtwoord)
?>
Khiem
  • 157
  • 6
  • 18
  • What happen if you write _WHERE id=$id_ instead of _WHERE id='$id'_ in update query – Niklesh Raut Mar 16 '16 at 13:07
  • Have you tried echoing out the $id value before you runt he SQL query? – Andreas Kruhlmann Mar 16 '16 at 13:08
  • Also what value you are getting in _echo $id=$_POST['id'];_ ? – Niklesh Raut Mar 16 '16 at 13:09
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – Jay Blanchard Mar 16 '16 at 13:11
  • @Niklesh I've tried that and didnt work. Where do I put the echo $id=$_POST['id']; ??? – Khiem Mar 16 '16 at 13:13
  • just inside _if(isset($_POST['btnUpdate'])) {_ – Niklesh Raut Mar 16 '16 at 13:14
  • only ` – Funk Forty Niner Mar 16 '16 at 13:19
  • your question doesn't contain a closing `` tag and no way of knowing if your submit button is even firing and you tagged as jQuery/JS with no code to support the question. Good luck. – Funk Forty Niner Mar 16 '16 at 13:29
  • @Fred-ii- It does contain a closing . Like I said when I change WHERE id='id' to a specific ID, it will update the record. I just did't put the whole code above, because I know those are not relevant. It has to do something with the WHERE id='id' .. – Khiem Mar 16 '16 at 13:30
  • `$id=$_POST['id'];` => `$id=$_GET['id'];` or `$id=$_REQUEST['id'];` since you're "fetching" from db. – Funk Forty Niner Mar 16 '16 at 13:31
  • @Fred-ii- both didn't work... I just added the whole code. – Khiem Mar 16 '16 at 13:42
  • You're using `$result1 = mysqli_query($connect, $sql);` and then `$result=$db->query($sql);` for the UPDATE using `$db` as the variable, and `$connect` in another. So, which one is the right variable? – Funk Forty Niner Mar 16 '16 at 13:44
  • If your SELECT works, then `$result=$db->query($sql);` would only make sense to use `$result=$connect->query($sql);` if `$connect` is your db variable to connect with, *n'est-ce pas?* – Funk Forty Niner Mar 16 '16 at 13:49
  • Right, I just added the connect from another tutorial I've found in order for me to get the values from the database in a dropdown list. I originally use db to connect to my database. I Will add connect.php to my post. – Khiem Mar 16 '16 at 13:50
  • @Khiem You have my answer below. – Funk Forty Niner Mar 16 '16 at 13:53
  • @Khiem what I think is happening here is that you may have lost the connection going from `mysqli_` and then switching over to PDO, which I have no idea why you're doing that. Use the same connection API that you already established with the `mysqli_connect()`. I've made an edit to my answer about that. – Funk Forty Niner Mar 16 '16 at 14:07

3 Answers3

1

Try with this,

Provide name to select tag not to option.

 <select name="id">
    <?php while ($row1 = mysqli_fetch_array($result1)):;?>
    <option value="<?php echo $id?>"><?php echo $row1[1];?> / <?php echo $row1[3];?> / <?php echo $row1[2];?> / <?php echo $row1[4];?></option>
    <?php endwhile;?>
</select>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

Besides the other answer given in regards to naming the <select>, instead of <option>, something to which I commented about myself.

(Now) Seeing what you're using to connect with, and added in a later edit:

$db=new PDO("mysql:host=localhost;dbname=i3a", "root", "");

and you're using mysqli_* to query with.

Those different MySQL APIs do NOT intermix. You MUST use the same one from connecting to querying.

So... either use all PDO or all mysqli_, nothing else.

So, instead of changing your entire code to PDO (that's up to you), you need to connect with using mysqli_.

I.e.:

$db = mysqli_connect("yourHost", "User", "Password", "DB");

The $db variable is what you need to use throughout your entire query.

I.e.:

  • $result1 = mysqli_query($db, $sql);
  • $result=$db->query($sql);

Having used error checking and error reporting, would have thrown you something about that.

Sidenote: What is happening here, is that you are most likely losing your connection in having to reconnect in the other query.

So, keep the connection open all the time without having to switch from one API to the other.

You already connected with:

$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "i3a";
$connect = mysqli_connect($hostname, $username, $password, $databaseName);

So why switch to PDO using another connection?

Keep the same one (mysqli_) and use $connection then if you're going to use the above to keep the connection alive.

  • $result1 = mysqli_query($connection, $sql);
  • $result=$connection->query($sql);

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

and check for errors on the queries.

You're not doing that.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • That's what i've found on the internet. When I change PDO to mysqli in connect.php, my other pages wont work... Sorry I'm just a beginner and trying to understand. I will add another example of my page where I get errors because of the change. – Khiem Mar 16 '16 at 14:09
  • @Khiem change `while($keuze=$menu->fetch()){` to `while($keuze=$menu->fetch_assoc()){` after seeing the error you posted in the question. – Funk Forty Niner Mar 16 '16 at 14:16
  • @Khiem Also `$menu= $db->query($sql);` you're still using `$db` here.... Use the SAME variable as per your connection and for ALL your queries being `$connection` in `$connect = mysqli_connect($hostname, $username, $password, $databaseName);` so `$menu= $connect->query($sql);` – Funk Forty Niner Mar 16 '16 at 14:18
  • That worked! But still can't get my problem solved? Any idea .. :( – Khiem Mar 16 '16 at 14:19
  • @Khiem again, check for errors as outlined in my answer and use `var_dump();` to see what's going in or not, and also view your HTML source, which is an additional developer's tool. There isn't much else I can do here for you, unless I setup an entire db for this. Sorry, but I can't do that. – Funk Forty Niner Mar 16 '16 at 14:21
  • Ugh. Thank you for helping me so far, I appreciate it! – Khiem Mar 16 '16 at 14:39
  • @Khiem You're welcome. I take it that you still haven't fixed the entire problem then(?). – Funk Forty Niner Mar 16 '16 at 14:40
  • Yes, I've been messing around and searching everywhere and still can't get this fixed. This is the only problem I'm having. I'm pretty sure it has to do something with the $id part. I Will update my code above. – Khiem Mar 16 '16 at 14:42
  • @Khiem Something else I noticed. This ` – Funk Forty Niner Mar 16 '16 at 14:48
  • I've changed both and still no succes. So frustrating .. :( – Khiem Mar 16 '16 at 14:52
0

Got it solved by adding this in value :

<option value="<?php echo $row1[0]; ?>">
Khiem
  • 157
  • 6
  • 18