Advertisement

09.29.2008 at 12:16AM PDT, ID: 23770571 | Points: 500
[x]
Attachment Details

PHP SQL statement works strangely

Asked by leew in PHP and Databases, PHP Scripting Language, MySQL Server

Tags:

Ok, I'm trying to do a "proof of concept" project.  So let me start off by saying I don't know PHP, I know a little bit of MySQL, and enough Linux to be dangerous.

So I have this page I put together that pulls data out of the MySQL database and displays it with a button that says "Approved".  When you click approved, it should update two fields in the database and then forward to another page.

Now here's where it gets strange.  Sometimes it works, sometimes it doesn't and it seems to depend on if and where I put the line:
echo $sql;

For example, this results in an error:
if($_POST[action]=="save")
{
$sql="UPDATE guests SET approvedon = NOW(), approved = 1 WHERE guestid = ".$gid.";";
$result=mysqli_query($connection,$sql);
header('location:http:/approve.php');
echo $sql;
}


But this works
if($_POST[action]=="save")
{
$sql="UPDATE guests SET approvedon = NOW(), approved = 1 WHERE guestid = ".$gid.";";
echo $sql;
$result=mysqli_query($connection,$sql);
header('location:http:/approve.php');
}

And this doesn't
if($_POST[action]=="save")
{
$sql="UPDATE guests SET approvedon = NOW(), approved = 1 WHERE guestid = ".$gid.";";
$result=mysqli_query($connection,$sql);
header('location:http:/approve.php');
}

The full code is in the snippet below with a couple of slight modifications for privacy.  Also, the error message received is:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
(which is why I'm trying to echo the SQL variable to review what it's seeing.



To be clear - HOW DO I GET RID OF THIS ERROR?  And can you add any comments on my "technique" or what I could do better.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
<?php
$host="localhost";
$user="root";
$password="";
$db="demo";
$gid=$_GET[gid];
if($gid=="")
{
        $gid=$_POST[gid];
}
$connection=mysqli_connect($host,$user,$password,$db);
if($_POST[action]=="save")
{
$sql="UPDATE guests SET approvedon = NOW(), approved = 1 WHERE guestid = ".$gid.";";
$result=mysqli_query($connection,$sql);
header('location:http:/approve.php');
echo $sql;
}
?>
<html>
<head>
</head>
<body>
<p align="center"><font size=5><b>Approve Guest</b></font></p>
<?php
 
$sql="select firstname, lastname, lastupdate, lastupdateby, track, webbio, progbookbio, url1, url2, url3, url4 from guests where guestid = ".$gid;
$result=mysqli_query($connection,$sql);
if($result==false)
{
        echo "<h4>Error: ".mysqli_error($connection)."</h4>";
}
else
{
?>
<table>
<tr>
        <td>Guest</td>
        <td>
        <?php
        $row_array = mysqli_fetch_row($result);
        echo $row_array[1].", ".$row_array[0];
        ?></td>
</tr>
<tr>
        <td>Last Update</td>
        <td><?php echo $row_array[2]." by ".$row_array[3] ?></td>
</tr>
<tr>
        <td>Track</td>
        <td><?php echo $row_array[4] ?></td>
</tr>
<tr>
        <td>Web Biography</td>
        <td><?php echo $row_array[5] ?></td>
</tr>
<tr>
        <td>Program Book Biography</td>
        <td><?php echo $row_array[6] ?></td>
</tr>
<tr>
        <td>URL 1</td>
        <td><?php echo $row_array[7] ?></td>
</tr>
<tr>
        <td>URL 2</td>
        <td><?php echo $row_array[8] ?></td>
</tr>
<tr>
        <td>URL 3</td>
        <td><?php echo $row_array[9] ?></td>
</tr>
<tr>
        <td>URL 4</td>
        <td><?php echo $row_array[10] ?></td>
</tr>
</table>
<?php
 
        for($i = 0; $i < mysqli_num_rows($result); $i++)
        {
                echo "<tr>";
                $row_array = mysqli_fetch_row($result);
                for($j = 0;$j < mysqli_num_fields($result);$j++)
                {
                        echo "<td>".$row_array[$j]."</td>\n";
                }
        }
}
echo "<form method='post' action='approve.php?gid=".$gid."'>";
echo "<input type='hidden' name='gid' value='".$gid."'>";
echo "<input type='hidden' name='action' value='save'>";
?>
<input type="submit" value="Approve">
</form>
</body>
</html>
[+][-]09.29.2008 at 12:30AM PDT, ID: 22594413

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.29.2008 at 12:33AM PDT, ID: 22594424

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.29.2008 at 12:45AM PDT, ID: 22594461

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.29.2008 at 12:48AM PDT, ID: 22594470

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.29.2008 at 12:58AM PDT, ID: 22594499

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.29.2008 at 01:45AM PDT, ID: 22594652

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.29.2008 at 01:53AM PDT, ID: 22594690

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.29.2008 at 05:19AM PDT, ID: 22595557

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628