Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hey guys I've been trying to do a code where I submit a form from a PHP Order Page so that it can update the MySQL Database with the required information

I've got the Order PHP Code Here:

<form action="http://zim.cs.uow.edu.au/~ga420/order.php" method="post">
    <tr>
        <th>Shirts</th>
        <th>Quantity</th>
    </tr>
    <tr>
        <td>
            <br />
            <input type="checkbox" name="items" value="SH01" />
            <label for="rd1">Obey T-Shirt: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH02" />
            <label for="rd1">Obey Professor: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH03" />
            <label for="rd1">Hustle T-Shirt: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH04" />
            <label for="rd1">Hip-Hop Support: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH05" />
            <label for="rd1">90's Shirt: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH06" />
            <label for="rd1">DOPE Shirt: $9.99</label>
            </div>
            <br />
            <br />
        </td>
        <td>
            <br />
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <br />
        </td>
    </tr>
    <tr>
        <td>
            <br />
            <input type="checkbox" name="items[]" value="SO1" />
            <label for="rd1">Shoe - Red Lace: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO2" />
            <label for="rd1">Shoe - Red High Top: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO3" />
            <label for="rd1">Shoe - White: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO4" />
            <label for="rd1">Shoe - Black: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO5" />
            <label for="rd1">Shoe - Black High Top: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO6" />
            <label for="rd1">Red Basketball: $19.99</label>
            </div>
            <br />
            <br />
        </td>
        <td>
            <br />
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <br />
        </td>
    </tr>
    <tr>
        <td>
            <br />
            <input type="checkbox" name="items[]" value="SN1" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN2" />
            <label for="rd1">Snapback: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN3" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN4" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN5" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN6" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <br />
        </td>
        <td>
            <br />
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <br />
        </td>
    </tr>
    </tr>
    </table>
    <br />
    <input type="submit" name="submit">
</form>

<?php

if (isset($_POST['submit'])){

    $conn = mysql_connect('url','username','password');
    if (!$con){
        die("Could Not Connect: " . mysql_error());
    }

    mysql_select_db("db",$conn);

    $sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,$_POST[items]','$_POST[qty]')";

    mysql_query($sql, $con);

mysql_close($con);
}

?>

Obviously my username and password I won't display but when I hit the submit button it says that it cannot connect.

You can try the form yourself on this website: http://zim.cs.uow.edu.au/~ga420/order.php

How come it is saying that I can't connect when clearly I have been putting the right details in.

Can anyone help? this has been stressing me out :'(

Your help is appreciated greatly!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
116 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to serialize all your inputs/quantities if you will have the same name "qty" in all fields. I will update my answer later if you do t get answer about this.

You had also small bugs on your code, notice this:

VALUES (null,'$_POST[items]','$_POST[qty]')"; - you were missing ' before $_POST[items]

$conn = mysql_connect('zim.cs.uow.edu.au','username','password'); - $conn with 2 "n"

So, a corrected version would be:

<?php

if (isset($_POST['submit'])){

$con = mysql_connect('url','username','password');
if (!$con){
    die("Could Not Connect: " . mysql_error());
}

mysql_select_db("db",$con);

$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,'$_POST[items]','$_POST[qty]')";

mysql_query($sql, $con);

mysql_close($con);
}

?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...