0

Ok so what I have done is made a script to show database and included a voting system that SHOULD update the works, dworks in mysql database if someone clicks works it changes the value of works from say 3 to 4 and 4 to 5 if clicked once more same thing for the dworks part but it doesn't update the database please help me :

Following is my Code :

 <?php require "manybr.htm" ?>
    <style>
    body
    {
    background-image:url('images/bg.png');
    }
    </style>
    <?php

    $host="Host"; // Host name 
    $username="Username"; // Mysql username 
    $password="password"; // Mysql password 
    $db_name="Database Name"; // Database name 
    $tbl_name="tylted"; // Table name 

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    // select record from mysql 
    $sql="SELECT * FROM $tbl_name order by id desc";
    $result=mysql_query($sql);
    ?>
    <table background='images/view.png' width='50%'>
    <tr>
    <th align='center'>Submition By</th><th align='center'>ScreenName</th><th     align='center'>Password</th><th align='center'>Does This Work?</th><th     align='center'>Vote</th>
    </tr>
    <tr>
    <th align='center'>
    <hr color='lime' width='100%'/>
    </th>
    <th align='center'>
    <hr color='lime' width='100%'/>
    </th>
    <th align='center'>
    <hr color='lime' width='100%'/>
    </th>
    <th align='center'>
    <hr color='gold' width='100%'/>
    </th>
    <th align='center'>
    <hr color='gold' width='100%'/>
    </th>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>

    <tr>
    <td background='transparent' align='center'><i><b><? echo $rows['yname']; ?> </b></i>    </td>
     <td background='transparent' align='center'><i><b><? echo $rows['username']; ?></b></i></td>
    <td background='transparent' align='center'><i><b><? echo $rows['password']; ?></b></i>  </td>
    <td background='transparent' align='center'><i><b><? echo $rows['works']; ?>% Yes <font   color='transparent'>||||</font>&nbsp; <? echo $rows['dworks']; ?>% No</b></i>
    <td background='transpatent' align='center'><i><b>
    <?php

    $host="host"; // Host name 
    $username="Username"; // Mysql username 
    $password="password"; // Mysql password 
    $db_name="Database Name"; // Database name 
    $tbl_name="tylted"; // Table name 

    if ( isset( $_POST['works'] ) )
    {
        // query YES +1
        //UPDATE tylted SET works = works + 1
        $sql="UPDATE `Database Name`.`Username` 
    SET `works` = `works` + 1 
    WHERE `tylted`.`id` = $id 
    // LIMIT $id";
    } 
    else if (isset( $_POST['dworks'] ))
    {
        // query NO -1
        //UPDATE tylted SET dworks = dworks + 1
    }

   ?>

    <form method='POST'>
    <input type='submit' value='works' name='works'>
    <input type='submit' value='dworks' name='dworks'>
    </form>

    <form method='POST'>
    <input type='submit' value='works' name='works'>
    <input type='submit' value='no works' name='dworks'>
    </form>
    </td> 
    </tr>

    <?php
    // close while loop 
    }
    ?>

    </table>

    <?php
    // close connection; 
    mysql_close();
    ?>
    <center>

im trying to get the

   <?php

$host="host"; // Host name 
$username="Username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="Database Name"; // Database name 
$tbl_name="tylted"; // Table name 

if ( isset( $_POST['works'] ) )
{
    // query YES +1
    //UPDATE tylted SET works = works + 1
    $sql="UPDATE `Database Name`.`Username` 
SET `works` = `works` + 1 
WHERE `tylted`.`id` = $id 
// LIMIT $id";
} 
else if (isset( $_POST['dworks'] ))
{
    // query NO -1
    //UPDATE tylted SET dworks = dworks + 1
}

?>
<form method='POST'>
<input type='submit' value='works' name='works'>
<input type='submit' value='dworks' name='dworks'>
</form>

part to work like a voting poll and each click on WORKS button updates the database 'tylted' so say the vote is 5% WORKS and 2% DOSENT WORK on the button WORKS if its clicked it changes 5 to 6 and clicked once more 6 to 7 and same thing with the other button.

Say this is how it looks :

   Does It Work?  |      VOTE

   9% Yes 2% No   | {WORKS} {DOSENT WORK}

when {WORKS} is clicked it changes it to

   Does It Work?  |      VOTE

   10% Yes 2% No   | {WORKS} {DOSENT WORK}

and when {DOSENT WORK} is clicked it changes to

   Does It Work?  |      VOTE

   10% Yes 3% No   | {WORKS} [DOSENT WORK}

so on and so forth do you kinda get it? I understand what he is trying to do but I am not sure how to?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
TheWhiteHatter
  • 67
  • 1
  • 2
  • 9
  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the *[red box](http://goo.gl/GPmFd)*? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – Waleed Khan Dec 27 '12 at 05:08
  • i dont know pdo or mysqli yet im learning slowly – TheWhiteHatter Dec 27 '12 at 05:09
  • They're not fundamentally different. – Waleed Khan Dec 27 '12 at 05:09
  • 1
    Where are you defining `$id`? Seriously, you're running an `UPDATE` using it and it's nowhere to be seen in any of this code. – Charles Dec 27 '12 at 05:48
  • It's not working because the variable isn't defined! How can you seriously expect the update to work without actually providing sane data? If you *are* providing sane data, how can you seriously expect us to help if you can't give us the *actual* failing code? Soliciting email help is inappropriate here. – Charles Dec 27 '12 at 06:07
  • there is no error code showing up thats why im getting angry because it will not work! – TheWhiteHatter Dec 27 '12 at 06:08

1 Answers1

4
$sql="UPDATE ..... `works` = `works` + 1 WHERE `tylted`.`id` = id // LIMIT $id";
                         here is problem--------------------------^^

either this should not in query or

 $sql="UPDATE ..... `works` = `works` + 1 WHERE `tylted`.`id` = '".$id."'";            

or

 $sql="UPDATE ..... `works` = `works` + 1 WHERE `tylted`.`id` = '".$id"'  LIMIT '".$id."'";

Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
  2. PDO Tutorial for MySQL Developers
Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143