Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head></head>
- <?php
- //$message=$_GET["message"];
- if (isset($_GET["message"])){
- $message=$_GET["message"];
- if($message!=""){
- echo "<script>alert('$message')</script>";
- }
- }
- ?>
- <body>
- <!--method="get" is the default -->
- <form method="post" action="Chap27_proc.php">
- <label>Name:</label><input type="text" name="txtName"><br>
- <label>Email: </label><input type="email" name="txtEmail"><br>
- <input type="submit">
- <button type="submit">Go</button>
- </form>
- </body>
- </html>
- //PART1 ENDS
- //PROC PAGE BEGINS
- <?php
- if (isset($_POST["txtName"])) {
- //$_POST is a global value
- //won't get here the first time you visit the page
- //will only get if a form has been submitted via post
- $name=$_POST["txtName"];
- $email=$_POST["txtEmail"];
- $msg= $name." ".$email;
- //Making a db conn
- define ("DB_HOST", "localhost");
- define("DB_USER", "root");
- define("DB_PASS", "");
- define("DB_NAME", "productsdemo");
- global $con;
- $con=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);//this creates conn to database
- if (!$con)
- {
- //die command makes sure that this process ends
- die('Could not connect: ' . mysql_error());//if could not connect
- }
- $sql="select * from products";//try not to Select * from
- if($result = mysqli_query($con, $sql)){
- $msg=$msg. "<BR>";
- while($row= mysqli_fetch_array($result)){
- $msg=$msg. $row["ID"] ." ".$row["Category"]. "<BR>";
- }
- //echo mysqli_num_rows($result)."<BR>";//useful for getting number of rows
- } else{$msg=$msg. "something went wrong";}
- //insert statement
- $prodId = 15;
- $category="Sportswear";
- $description="Hockey Stick";
- $price=29.99;
- //INSERT
- $sql="Insert into Products(ID,Category,Description, Price, Image) values ($prodId,'$category','$description',$price,1)";
- $msg=$msg. $sql."<BR>";
- mysqli_query($con, $sql);
- if(mysqli_affected_rows($con)==1){$msg= "Insert success<BR>";}
- else{$msg=$msg. "Something went wrong";}
- //UPDATE
- $description="baseball bat";
- $sql="Update products set Description='$description' where ID=$prodId";
- mysqli_query($con, $sql);
- $msg=$msg. (mysqli_affected_rows($con)==1) ? "Updated<BR>":"Not Updated<BR>";
- //DELETE
- //$sql="delete from products where ID=$prodId";
- //mysqli_query($con, $sql);
- //echo (mysqli_affected_rows($con)==1) ? "Deleted<BR>":"Not Deleted<BR>";
- }//end BIG IF statement
- //a header redirect will send the user to another page
- header("location:Chap27.php?message=$msg");
- //this is important for sprint 1.
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement