Home mysqli_query() WHERE - insert data
Reply: 2

mysqli_query() WHERE - insert data

Matej Hanzlić
Matej Hanzlić Published in 2018-01-13 07:11:17Z

i'm new to php and what im trying to do here is add rating +1 for specific id (1,10,30....) whenever i press arrow up button.

I need to add id value (echo $row['id'];) to mysqli_query() somehow. Can someone help please? :)

<?php include("db.php")?>
$sql = "SELECT * FROM gifs";
$result = mysqli_query ($db,$sql);
//Fetch data from database
if ($result -> num_rows >0){

    while($row = mysqli_fetch_array($result)){

      <div><?php echo $row["name"];?></div>

            <p id="<?php echo $row['id'];?>">
            <?php echo $row['copygif'];?></p>

$add = $row['id']; //i know this dosnt work...
<i onClick="<?php mysqli_query($db,"UPDATE gifs SET rating = rating + 1 WHERE id= $add ");?>"></i>

How do i translate ($row['id'];) into mysqli_query( id = ?)

Thank You

RamRaider Reply to 2018-01-13 07:37:55Z

Very quickly cobbled together to show how you might accomplish your goal. You cannot simply embed the sql query as you did - the PHP code runs when the page loads ( it being server side code ) so will execute before you see any content on the page. The event click is a clientside event so you need some mechanism to send the request from the client to the server - typically one might use ajax or, as here, a form submission. The code below was not tested so you might find little mistooks but the idea should help solve the issue - this though is only one method you could employ.

    /* include db connection */

    /* Process form submission */
    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['id'] ) ){


        /* sql query for prepared statement */
        $sql='update gifs set rating = rating + 1 where id=?;';

        /* prepare the sql query */
        $stmt=$db->prepare( $sql );
        if( $stmt ){

            /* bind the placeholder to the id & execute */

        } else {
            exit( 'error: failed to prepare sql' );

    /* select recordset for display */
    $sql = "select * from `gifs`;";
    $result = $db->query( $sql );

<!doctype html>
        <meta charset='utf-8' />
            /* assign event listeners to `i` elements */

                var form=document.forms['gifs'];
                var id=form.querySelector('input[type="hidden"][name="id"]');
                var col=Array.prototype.slice.call( form.querySelectorAll('i.vote') );

                        /* set the value of the hidden field to the data-id value of current `i` */
                    }.bind( node );
            },false );
        <!-- add a basic form, using POST -->
        <form name='gifs' method='post'>
            <!-- hidden field for ID value -->
            <input type='hidden' name='id' />

                /* display recordset and create `i` elements that will register vote click */
                if( $result->num_rows > 0 ){
                     while( $row = $result->fetch_object() ){
                        echo "
                            <p id='{$row->id}'>{$row->copygif}</p>
                            <i class='vote' data-id='{$row->id}'>vote</i>";
NIMI Reply to 2018-01-13 08:13:18Z

yes as @RamRaider mentioned in the comment above you may need to bind a function on up arrow button and pass the $row['id'] in i, and onclick on the button you can send this id via ajax to server and do what you to. Example may look like,

<?php while($row = mysqli_fetch_array($result)){
  <div><?php echo $row["name"];?></div>

        <p id="<?php echo $row['id'];?>">
        <?php echo $row['copygif'];?></p>
         $add = $row['id'];
         <i onClick="upButtonClick($row['id'])"></i>
<?php } ?>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306051 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO