开发者

Only inserts in to database once then fails

I have a small section of code. When the table is empty this code works fine and enters in to the table fine. But then if i try again then this fails with the error?

What am i doing wrong?

Thanks

// On my Function page

    function admin(){
                    connect();

                    $query = mysql_query("INSERT INTO results 
                    (t_id, pos1, pos2, pos3)
                    VALUES  ('$_POST[t_id]','$_POST[pos1]','$_POST[pos2]','$_POST[pos3]')")
                    or die ("Error.");

                    $b = "Updated fine</b></a>.";

                    return $b;
                        exit();
            }

    // Then on my main page

<?php
    include ('functions.php');
    if (isset($_POST['admin'])){
    $admin = admin();
    }
?>

            <div id="content">
                    <div id="admin">
                        <form action="" method="post">
                            <table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
                                <tr>
                                    <td width="100%"><?php echo "$admin"; ?></td>
                                </tr>
                                <tr>
                                    <td width="100%"><label>Track <input type="text" name="track" size="25" value="<? echo $_POST[t_id]; ?>"></label></td>
                 </tr>
                                <tr>
                                    <td width="100%"><label>Position 1<input type="text" name="pos1" size="25" value="<? echo $_POST[pos1]; ?>"></label></td>
                                </tr>
                                <tr>
                                    <td width="100%"><label>Position 2 <input type="text" name="pos2" size="25" value="<? echo $_POST[pos2]; ?>"></label></td>
                                </tr>
                                <tr>
                                    <td width="100%"><label>Position 3 <input type="text" name="pos3" size="25" value="<? echo $_POST[pos3]; ?>"></label></td>
                                </tr>
                                <tr>
                                    <td width="100%"><input class="save" type="submit" value="" name="admin"></td>
                         开发者_高级运维       </tr>
                            </table>
                        </form>
                    </div>
                </div>


Without seeing your table schema, I can only think you have UNIQUE t_id and you want to insert the same ID into it.

Several way to debug:

  • Use or die ("Error: " . mysql_error()); instead of just or die ("Error.");
  • Check your table schema: SHOW CREATE TABLE tablename and write it down on your question, so we can see if it's causing error.


It is hard to guess. Maybe you are entering the same values twice, and they happen to violate some unique constraint?

But you make another mistake: you forget to call mysql_real_escape(). That is bad.


Can you tell us of the error? It sounds like you're hitting a primary key violation, perhaps by trying to insert the same id more than once.

That aside, your code is riddled with security holes.

You should not be inserting variables straight from the POST into your query. All I have to do is submit '; DROP DATABASE and I can completely wreck your system.

Additionally, you're injecting values directly from POST into input fields, meaning I can set up a button on my site that submits " <script type='text/javascript'>window.location='http://mysite.com'</script> or something along those lines and take over your page.

This may sound terse, but you should do some googling or pick up a book regarding textbook security issues with websites.

EDIT: Just saw your comment about learning security. My advice is to be proactive about this sort of thing, because being reactive is often too late to fix problems.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜