开发者

Problem with MySQL update [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

table updates empty spaces when user do not enter anything to the textbox

greetings :)

i am having problems updating my database whenever the user clicks on the submit button. i am going to show you the flow of my program,i already tried figuring out the problem,but i just can't find solutions. i hope someone could help me.

i have 2 problems encountered here:

  1. my database won't update after clicking the submit button
  2. the user may choose which to update,if the textbox is empty,it will update the data with empty spaces.and i want the data to remain as it is if the textbox is empty.

in my program,if you want to update the employee information,you must click the name that contains a link in the page. (in my program its the employee name that needs to be clicked) when clicked,a pop up will open.

the link in my index.php contains the following code:

<td class="sub" width="100" align="center">
  <a href="" onclick = javascript:newPopup('empinfo.php?emp=<?php echo $eid ?>');><?php echo$ename?></a>
    </td>

NOTE the empinfo.php is my pop up window,it calls the pop up when clicked. emp isthe name i assign to pass in the empinfo.php it contains the employee ID. NO PROBLEM HERE,I JUST WANT TO SHOW YOU THE FLOW

when the empinfo.php appears,it will show this format:

Employee name: //textbox here
Position:      /textbox here
Department:    /textbox here
Employee Tag:  /textbox here
**SUBMIT BUTTON**

when the user clicks the submit button, it should have updated the database with the inputted values,but mine won't update :(

here is the codes i used:

<?php
$con=mysql_connect('localhost','root','mariel') or die(mysql_error());
mysql_select_db('intranet',$con);
if(isset($_POST['submitted']))
    {
    $qry = "UPDATE gpl_employees_list SET emp_nme = '".$_POST['name']."', emp_pos = '".$_POST['pos']."', emp_dep = '".$_POST['dep']."', emp_tag = '".$_POST['tag']."' WHERE emp_id = '".$_GET['emp']."' ";  
    mysql_query($qry) or die (mysql_error());
    }
?>

this is the content code in my form,together with the submit that i used:

<form action="index.php" method="POST">
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='eid' id='eid' value= '<?php echo $_GET['emp']?>' />  

<fieldset>
            <div class='container'>
                <label for='ename' >Employee name:</label><br/>
                <input type='text' name='ename' id='ename' value='' maxlength="50" /><br/><br/>
             </div>

             <div class='container'>
                <label for='pos' >Position:</label><br/>
                <input type='text' name='pos' id='pos' value='' maxlength="50" /><br/><br/>
             </div>

            <div class='container'>
                <label for='dep' >Department/Division:</label><br/>
                <input type='text' name='dep' id='dep' value='' maxlength="100" /><br/><br/>
            </div>

            <div class='container'>
                <label for='tag' >Employee Tag:</label><br/>
                <select name="tag" id="tag">
                    <option value="Y">Yes</option>
                    <option value="N">No</option>
                </select> <br/><br/>
            </div>

            <div class='container'>
                <input type='submit' name='Submit' value='Submit' onclick = "location.reload();window.close()"/>
            </div>
</fieldset>     
</form>

i hope someone could clear it up for me

Mi开发者_开发技巧saChan


It's not updating because you probably need to refer to $_POST['eid'] instead of $_GET['emp'] because you don't have it in index.php like index.php?emp=1. You already have that field so use that:

<input type='hidden' name='eid' id='eid' value= '<?php echo $_GET['emp']?>' />

Also you don't need to do this:

onclick = "location.reload();window.close()"

Type submit reloads the page by default. Lastly, consider @Sam152's pointers :)


There could be a number of things wrong, but these points should help you debug your script.

  • Firstly you need to escape your post variables to ensure things like apostrophes don't mess up your query, it's also a security vulnerability.
  • Secondly, make sure your form action is pointing to the PHP script. Maybe put a print statement at the top of the script to make sure PHP is actually receiving the data.
  • Then assign the value of the SQL query to a variable and print it out before you run it. You can then easily see what's being sent to the SQL server. Maybe run it in an SQL management tool my phpMyAdmin and observe any errors with it.

Hope this helps. Feel free to update your question with new information as it comes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜