Help, Not sure how to go about inserting data.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zanussi70
Forum Newbie
Posts: 4
Joined: Thu Apr 09, 2009 11:39 am

Help, Not sure how to go about inserting data.

Post by zanussi70 »

Hi, i've started using php for a project for school and I have to create an online shop as it were. There are three tables customer, stock and order.

Customer Fields are :CustomerID Lastname Firstname Username Password Address1 Address2 Postcode Phone Email

Order fields are : OrderID CustomerID ProductID OrderDate Text Font Picture Filled

Stock fields are : ProductID ProductName StockAmount PricePerTshirt

So i've been trying to create an order form. Wherein customers can
  • put a hyperlink to an image
    put in text that they want on their tshirt
    choose the font that they want on their tshirt
    and choose which type (which is just sizes and whether they're male or female.)
So to try and do this I made the code which i'm posting below. This seems to work okay but then my real problems lie within the next page where i'm trying to get the information into the database. I have limited access to the server( in fact i won't be able to try any code for another week). I was hoping someone would look at the second part of my project and know if it will work in putting information into my database. And if it won't could you suggest some code that might work. Thank you to anyone willing to help.

PART ONE named Placeorder.php

Code: Select all

<html>
<head>
<title>Home Printed Tshirts</title>
</head>
<body>
<h3>Order a Black Confetti Home Printed Tshirt!</h3>
<h4>Order Form </h4>
<table>
  <form name="frmPlaceorder" action="AddOrder.php" method="post">
    <tr>
      <td>Select Font and Tshirt size in the Form Below</td>
    </tr>
    Hyperlink to Picture: <input name="hyperlink" type="text" /><br />
    Text to be on your Tshirt(This can be no longer than 50 Characters.(that means spaces are included.):<br />
    <textarea name="text_on_tshirt" rows="15" cols="40"></textarea>
    <br/>
    <tr>
    <td><select name="ChooseFont">
    <option value="Arial Black"Arial Black</option>;
<option value="Verdana"Verdana</option>;
<option value="Comic Sans MS"Comic Sans MS</option>;
<option value="Calibri"Calibri</option>;
<option value="Courier New"Courier New</option>;
<option value="Cambria"Cambria</option>;
<option value="Tahoma"Tahoma</option>;
<option value="Trebuchet MS"Trebuchet MS</option>;
<option value="Windings"Windings</option>;
<option value="Algerian"Algerian</option>;
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><select name="selectTshirt">
 

Code: Select all

<?PHP</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; //variable to check if no tshirts:</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; $flag = 1;</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; //connect to database to obtain list of all Ts'</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; $dbh = mysql_connect("localhost","afearon582","67tyu89");</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; mysql_select_db("e1420022_afearon582", $dbh); </li><li style=\"\" class=\"li2\">&nbsp; &nbsp; //create sql to list tshirts in order of product id</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; $sql = "SELECT * FROM stock WHERE StockAmount>'0' ";</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; $result=mysql_query($sql);</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; //no. of records found</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; $num=mysql_numrows($result);</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; //get &nbsp;details</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; if($num > 0) {</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; $i=0;</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; while ($i < $num) {</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get tshirt details</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ProductID = mysql_result($result,$i,"ProductID");</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ProductName = mysql_result($result,$i,"ProductName");</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add details to HTML listbox:</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<option value='". $ProductID . "'>".$ProductName."</option>";</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add details to HTML listbox:</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; }</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; else {</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; //no tshirts found:</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; &nbsp; echo "<option value=''>No Tshirts In Stock!!</option>";</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; $flag = 0;</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; }</li><li style=\"\" class=\"li2\">&nbsp; &nbsp;?></li><li style=\"\" class=\"li1\">&nbsp; &nbsp; //close database</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; mysql_close();</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?></li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; </select></td></li><li style=\"\" class=\"li1\">&nbsp; &nbsp; </tr></li><li style=\"\" class=\"li2\">&nbsp; &nbsp; <tr></li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; <td>&nbsp;</td></li><li style=\"\" class=\"li2\">&nbsp; &nbsp; </tr></li><li style=\"\" class=\"li1\">&nbsp; &nbsp; <tr></li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; <td><a href="http://afearon582.methody.org/home/menu.php">Cancel and Return to Main Menu </a></td></li><li style=\"\" class=\"li1\">&nbsp; &nbsp; </tr></li><li style=\"\" class=\"li2\">&nbsp; &nbsp; <?php</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp;</li><li style=\"\" class=\"li2\">&nbsp; &nbsp; &nbsp; &nbsp; if($flag == 1) {</li><li style=\"\" class=\"li1\">&nbsp; &nbsp; ?></li><li style=\"\" class=\"li2\">&nbsp; &nbsp; <tr></li><li style=\"\" class=\"li1\">&nbsp; &nbsp; &nbsp; <td><input type='submit' name='Submit' value='Click to Continue' /></td></li><li style=\"\" class=\"li2\">&nbsp; &nbsp; </tr></li><li style=\"\" class=\"li1\">&nbsp; &nbsp; <? } ?>
  </form></table></body></html>

Part Two named AddOrder

Code: Select all

<?PHP
    session_start(); 
    header("Cache-control: private");
        
    //get new order details:
    $ProductID = trim($_POST['SelectTshirt']);
    $OrderDate= trim($_POST['txtOrderDate']);
    $Text = trim($_POST['text_on_shirt']);
    $Font = trim($_POST['ChooseFont']);
    $Picture = trim($_POST['hyperlink']);
    
        
    //connect to database and add new customer details
    $dbh = mysql_connect("localhost","afearon582","67tyu89") or die('Sorry, could not connect:'.mysql_error());
 
    mysql_select_db("e1420022_afearon582", $dbh); 
    //check if needing to add a new customer, or amend existing customer
 
    $sql = "INSERT INTO order VALUES(NULL, '$ProductID',CURDATE(),'$Text','$Font','$Picture')";
    
    
    if($result=mysql_query($sql)) 
    {
        echo "<br />Order successfully added!";
    }
    else 
    {
        echo "<br />Sorry - Order not added! Reason: " . mysql_error();
        //close database
        mysql_close();
    }
    header("Location: index2.php");
?>
Any more information needed please just say.
zanussi70
Forum Newbie
Posts: 4
Joined: Thu Apr 09, 2009 11:39 am

Re: Help, Not sure how to go about inserting data.

Post by zanussi70 »

oh and if i've said or done anything stupid i'm sorry, i am very new at this.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Help, Not sure how to go about inserting data.

Post by yacahuma »

you dont have a computer? If you do, you can just install mysql on your machine and test all your code there?

How far do you want to take you project? There are many things you dont mention like an admin page and shopping cart?
zanussi70
Forum Newbie
Posts: 4
Joined: Thu Apr 09, 2009 11:39 am

Re: Help, Not sure how to go about inserting data.

Post by zanussi70 »

I do have a computer, how would installing mysql allow me to test my code? how would that work?

The current way i do things is I ftp into a file server and put my php files up that way and then look at the site. could i use mysql to test my code another way?
There's no admin page and no shopping cart. It's the most basic of systems. Literally i just want to take the information inputted from Placeorder.php and put it into the database and i don't know how to do that.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Help, Not sure how to go about inserting data.

Post by yacahuma »

in order to run php , you need a web server(apache), mysql and php. If I were you I would have my development environment on my machine, and when is ready upload to the server. If you want to keep developing with php , you should definitively consider having this programs setup in your own machine.

take a look at http://www.apachefriends.org/en/xampp.html
zanussi70
Forum Newbie
Posts: 4
Joined: Thu Apr 09, 2009 11:39 am

Re: Help, Not sure how to go about inserting data.

Post by zanussi70 »

Thank you. I've installed that.
Amandacrew
Forum Newbie
Posts: 1
Joined: Thu Jun 10, 2010 12:10 am

Re: Help, Not sure how to go about inserting data.

Post by Amandacrew »

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up! Big thanks for the useful info

Pretty insightful post. Never thought that it was this simple after all. I had spent a good deal of my time looking for someone to explain this subject clearly and you’re the only one that ever did that. Kudos to you! Keep it up

Cheap online Whole t-shirts
Post Reply