Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP problem again

  • 18-03-2008 12:36PM
    #1
    Registered Users, Registered Users 2 Posts: 272 ✭✭


    Hi again,

    I seem to be having a problem with my code, the sql statement seem fine when i run it without the form. but when i intro the form i get :"Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\wordpress\wp-content\plugins\exec-php\includes\runtime.php(41) : eval()’d code on line 28"

    also the java script alert wouldnt propmt even when i removed the sql statement....HELP



    <code>
    <script type="text/javascript">


    function setButton(myvalue)
    {
    alert("Change");
    document.getElementById("myButton").value="Click here to \nupdate '"+myvalue+"' in database";
    }
    </script>

    <?php
    // prepare and connect MySQL action
    $dbhost = 'test.test.com';
    $dbuser = 'audit';
    $dbpass = 'audit';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die('Error connecting to mysql');
    $dbname = 'WinAudi2';
    mysql_select_db($dbname);

    // Check to see if form was posted
    if (isset($_POST)) {
    // if so, do what you plan to do
    echo "Clicked and passed value: {$_POST}<br>";
    print "Update database<br>";
    print "Show table<br>";
    $result = mysql_query( "SELECT Computer, ItemName, ItemValue1 FROM $dbname WHERE Computer=$_POST") or die("SELECT Error: ".mysql_error());
    $num_rows = mysql_num_rows($result);
    print "There are $num_rows records.<P>";
    print "<table width=150 border=1>\n";
    while ($get_info = mysql_fetch_row($result)){
    print "<tr>\n";
    foreach ($get_info as $field)
    print "\t<td><font face=arial size=1/>$field</font></td>\n";
    print "</tr>\n";
    }
    print "</table>\n";
    }
    // form not posted: display selection box
    else {
    $result = mysql_query( "SELECT DISTINCT Computer FROM $dbname" )
    or die("SELECT Error: ".mysql_error());
    $num_rows = mysql_num_rows($result);
    print "<p>There are $num_rows records.</p>\n";
    print "Select an option from the following list:\n";
    print "<form method='POST' action='".$_SERVER."'}>\n";
    print "<select name='selectit' onChange='setButton(this.value);'> >\n";
    while ($get_info = mysql_fetch_row($result)) {
    foreach ($get_info as $field)
    print "<option value='$field'>$field</option>\n";
    }
    print "</select>\n";
    print "<p><input type='submit' value='   ' id='myButton'/></p>\n";
    print "</form>";
    }
    ?>

    </code>


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Well if that's a copy and paste, you're missing a comma at the start of the dbhost variable.


  • Registered Users, Registered Users 2 Posts: 272 ✭✭hannable80


    no there is one in there,


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Change this line:
    $result = mysql_query( "SELECT Computer, ItemName, ItemValue1 FROM $dbname WHERE Computer=$_POST") or die("SELECT Error: ".mysql_error());

    To this:
    $result = mysql_query("SELECT Computer, ItemName, ItemValue1 FROM $dbname WHERE Computer=".$_POST) or die("SELECT Error: ".mysql_error());


  • Registered Users, Registered Users 2 Posts: 272 ✭✭hannable80


    dam those semi colons :D
    That seems to have done the tric but for one error.



    Clicked and passed value: Computer1
    Update database
    Show table
    SELECT Error: Unknown column 'Computer1' in 'where clause'


    SQL error !!!


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Your query is coming out something like this:

    SELECT Computer, ItemName, ItemValue1 FROM $dbname WHERE Computer=Computer1

    That "Computer1" needs to be enclosed in single quotes, otherwise MySQL thinks you're referring to a column and not a static string value.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 272 ✭✭hannable80


    $result = mysql_query("SELECT Computer, ItemName, ItemValue1 FROM $dbname WHERE Computer=".$_POST) or die("SELECT Error: ".mysql_error());
    so how will i get around passing it in with single quotes around it ?

    Thanks for the help


  • Closed Accounts Posts: 25,848 ✭✭✭✭Zombrex


    hannable80 wrote: »
    SELECT Error: Unknown column 'Computer1' in 'where clause'
    As seamus says, change

    $result = mysql_query("SELECT Computer, ItemName, ItemValue1 FROM $dbname WHERE Computer=".$_POST) or die("SELECT Error: ".mysql_error());

    to

    $result = mysql_query("SELECT Computer, ItemName, ItemValue1 FROM $dbname WHERE Computer='" . $_POST ."'") or die("SELECT Error: ".mysql_error());

    your query will come out now as

    WHERE Computer='Computer1';

    Notice the single quotes on both sides.


  • Registered Users, Registered Users 2 Posts: 272 ✭✭hannable80


    well i tipp my hat at all of ye....It worked

    THANKS A MILL


Advertisement