0

I have this script but mysql insert section I have a problem the script didn't insert the first column "DNS" in sql table but did insert the second and the third one. what is my mistake the reason the first column escaped.

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $all = implode(",",$_POST);
    $all = explode(",",$all);

    $insert = "INSERT INTO Cname VALUES (";    

    for ($x=0;$x<3;$x++)
    {
        $all[$x] = clean($all[$x]);
        if ($all[$x] == "" || strlen($all[$x]) > 300)
            die("<p>Incorect Data Entry, Please check again! Column - " . $x);

        $insert .= "'" . $all[$x] . "',";
    }
    $insert .= "'')";

    $res = mysql_query($insert) or die(mysql_error());
    if($res)
        die("<p>Succesfully Added! <a href='index.php'>Back To View Page</a></p>");

} else {
    ?>
    <title>Add CNAME.</title>
    </head>
    <body>
    <?php include("nav.php");?>

    <div class="red"><p><h5>All fields are required.</h5></p></div>
    <table id="box-table-a"> 
    <tbody>
        <form action="" method="POST">
        <tr>
            <td>DNS</td>
            <!-- allow only the word CNAME  -->    
            <td><input type="text" name="DNS" placeholder="enter a word 'CNAME" required pattern="CNAME" title="enter a word 'CNAME' on this field"></td> 
        </tr>
        <tr>
            <td>name</td>
            <td><input type="text" name="name" placeholder="host with Full Domain Name" required  pattern="^[a-zA-Z0-9\-]*$" title="host with full Domain Name"></td>
        </tr>
        <tr>
            <td>alias</td>                                                                   
            <td><input type="text" name="alias" placeholder="only 3-15 digit are  allowed" required pattern="^[a-zA-Z0-9-_\-]{3,15}*$" title="only 3-15 digit are allowed"></td> 
        </tr>
        <tr>
            <td><input type="submit" value="Add Cname"></td>
            <td><input type="reset"></td>
        </tr>
    </tbody>
    </table>
    <?php
}
?>
3
  • 5
    dump out the generated query (echo $query) and see what you produced. don't assume your query generating code is working properly. always check outputs. beyond that, what's the point of your implode/explode at the start of the script? why not just $all = $_POST? You're trying to explode an array, not a string.
    – Marc B
    Commented Feb 19, 2013 at 18:09
  • There is a trailing , in your values list.
    – Musa
    Commented Feb 19, 2013 at 18:11
  • thanks Mark! (echo $query) display my error " the script expect the sql index(id) last column but my sql table (index) column was first. I changed that and now it's working as I expected.
    – eli1128
    Commented Feb 19, 2013 at 22:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.