1

I can't get the syntax right for this. I'm trying to get the $nproj_hours value that is sumitted via a form to look-up the hours_id key value associated to it in the hours table, and put that numerical value in the summary table in a new row (I'll be doing this for departments and projects as well, so if there's a way to wrap them all up into one, I have a projects and departments table too).

Final Code:

if (isset($_POST['btnnew']))  

    {
    echo "<pre>Value of \$_POST:</br>";print_r($_POST);echo"</pre>";

        $nclarity_id    = $_POST['nclarity_id'];
        $nproj_hours    = $_POST['nproj_hours'];
        $ndept_name     = $_POST['ndept_name'];
        $proj_id        = $_POST['nclarity_id'];
        $hours_id       = $_POST['nproj_hours'];
        $dept_id        = $_POST['ndept_name'];

        $sql        = "INSERT INTO `summary` VALUES (null,'$proj_id','$hours_id','$dept_id',null)" 
                            or die ("couldn't update".mysql_error());
        $query = mysql_query($sql);

//echo "<pre>Value of \$sql:</br>";print_r($sql);echo"</pre>";
        if ($query)
{
echo "success!";
}
else
            {
        die('error inserting new record'.mysql_error());
    } // end of the nested if statement
}

?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
   <tr>
   <td align="center">&nbsp;</td>
   <td align="center"><strong>Clarity ID</strong></td>
   <td align="center"><strong>Hours</strong></td>
   <td align="center"><strong>Department</strong></td>
   </tr>
<form name="form1" method="post" action="stupid.php">
<tr>
<td>&nbsp;</td>
<select name="nclarity_id">

<?php
$sql = "SELECT proj_id, clarity_id FROM projects " . "ORDER by clarity_id";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['proj_id']."\">".$row['clarity_id']."</option>\n  ";
}
?>

</select>
<select name="nproj_hours">
<?php
$sql = "SELECT hours_id, proj_hours FROM hours";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['hours_id']."\">".$row['proj_hours']."</option>\n  ";
}
?>
</select>

<select name="ndept_name">

<?php
$sql = "SELECT dept_id, dept_name FROM departments";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['dept_id']."\">".$row['dept_name']."</option>\n  ";
}
?>
</select>
   <input type="submit" name="btnnew" value="Enter New Record">
</tr>
</table>

<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="500" border="1" cellspacing="0" cellpadding="3">
</tr>


<?php
while($rows=mysql_fetch_array($res))
{

?>

<?php
}

?>

</table>
</td>
</tr>
</table>


<?php
mysql_close();
?>
Mike
  • 2,531
  • 6
  • 26
  • 34

1 Answers1

1

You have to change your query - specifically remove the keyword VALUES and add the column names. Refer the spec for the exact syntax and details. I have just put in colname1, colname2, colname3, colname4 - you need to replace that with the actual column names from your summary table

INSERT INTO summary (colname1, colname2, colname3, colname4) 
   SELECT NULL, NULL, proj_hours, NULL
   FROM hours 
   WHERE proj_id ='$nproj_hours

As per usual, the disclaimer to avoid using such queries due to possibility of SQL Injection is applicable. Please google sql injection / prepared statements / parameterized queries etc if you are not aware of the same.

Jagmag
  • 10,283
  • 1
  • 34
  • 58
  • What would be a better way to do this? Also, that didn't work. I got the following 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 'SELECT NULL,dept_name FROM departments WHERE dept_name = 'Busi' at line 2 – Mike Oct 06 '12 at 17:53
  • Refer this answer http://stackoverflow.com/a/60496/372871 . This has samples of using prepared statements and parameterized queries as well as an excellent explanation. Regarding why the above query didnt work, u probably need to provide details of the schema of the table you are trying to insert into to make more sense of why it didnt work. Also provide the exact query as well -the query u just posted in your comment - is different from the query in both your question / my answer in the no of parameters it has. – Jagmag Oct 06 '12 at 18:03
  • I have 4 tables: summary, projects, hours, and departments I have predefined values in the tables: projects, hours, and departments (each of them have an ID and value columns) I have a form where users are able to choose the actual values from the projects, hours, and departments tables. The summary table keeps the key ID's from each submission (what I'm trying to do). For example, if someone submits values of projects=test, hours=18.25, and departments=test I want the script to find the ID's of each value, from each table, and store that in the summary table. – Mike Oct 06 '12 at 20:03
  • I see. This is what I would do. Ideally in your UI if you are having the user select from predefined values of ` projects / hours and department` , usually u would be doing that using some kind of control like a `Drop down / select` box. If that is the case, usually such controls have a `TEXT` and a `VALUE`. So, i would store the text as what the user understands and the VALUE as its ID. For eg- If department "PHYSICS" has ID = 11 in the `departments` table, then the value stored in the UI for physics is 11. This way once u need to save, you just take all the VALUES (not the TEXT) from the UI – Jagmag Oct 07 '12 at 05:50
  • So, then, u have all your 4 VALUES from the UI and all u do is fire an INSERT with all the 4 values. You shouldnt need to fire a `SELECT` query again as you should ideally be storing the `ID's` of the projects / hours / departments in the summary table - NOT their actual values to avoid redundancy of data in your table. When you need to retrieve summary, all you do is INNER JOIN of SUMMARY with all 3 tables and you are good to go. – Jagmag Oct 07 '12 at 05:57
  • I need to leave for a bit, but will try later and reply! I get what you're saying, but I'm confused on one piece of that. That's the problem I've been having, which is how to show the value for the user, but actually store the ID... I've been able to show the value, but am unable to store the ID correctly. Can you give me a code example? Thank you for your help, by the way! – Mike Oct 07 '12 at 13:19
  • Got it! I'll update my code to reflect the change, but yes.. I added the ID with the value I needed and just referenced it there. I don't know why that eluded me, but whatever.. fixed now! Thanks! – Mike Oct 09 '12 at 11:31