Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head></head>
  4.     <?php
  5.     //$message=$_GET["message"];
  6.     if (isset($_GET["message"])){
  7.         $message=$_GET["message"];
  8.         if($message!=""){
  9.             echo "<script>alert('$message')</script>";
  10.             }
  11.        
  12.        
  13.     }
  14.  
  15.     ?>
  16.     <body>
  17.         <!--method="get" is the default -->
  18.         <form method="post" action="Chap27_proc.php">
  19.             <label>Name:</label><input type="text" name="txtName"><br>
  20.             <label>Email: </label><input type="email" name="txtEmail"><br>
  21.             <input type="submit">
  22.             <button type="submit">Go</button>            
  23.            
  24.         </form>
  25.        
  26.     </body>
  27. </html>
  28. //PART1 ENDS
  29. //PROC PAGE BEGINS
  30. <?php
  31. if (isset($_POST["txtName"])) {
  32.     //$_POST is a global value
  33.     //won't get here the first time you visit the page
  34.     //will only get if a form has been submitted via post
  35.     $name=$_POST["txtName"];
  36.     $email=$_POST["txtEmail"];
  37.    
  38.     $msg= $name." ".$email;
  39.    
  40.     //Making a db conn
  41.     define ("DB_HOST", "localhost");
  42.     define("DB_USER", "root");
  43.     define("DB_PASS", "");
  44.     define("DB_NAME", "productsdemo");
  45.    
  46.     global $con;
  47.         $con=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);//this creates conn to database
  48.     if (!$con)
  49.     {
  50.         //die command makes sure that this process ends
  51.     die('Could not connect: ' . mysql_error());//if could not connect
  52.     }
  53.     $sql="select * from products";//try not to Select * from
  54.    
  55.     if($result = mysqli_query($con, $sql)){
  56.         $msg=$msg. "<BR>";
  57.        
  58.         while($row= mysqli_fetch_array($result)){
  59.             $msg=$msg. $row["ID"] ." ".$row["Category"]. "<BR>";
  60.         }
  61.  
  62.         //echo mysqli_num_rows($result)."<BR>";//useful for getting number of rows
  63.        
  64.        
  65.     }    else{$msg=$msg. "something went wrong";}
  66.     //insert statement
  67.    
  68.     $prodId = 15;
  69.     $category="Sportswear";
  70.     $description="Hockey Stick";
  71.     $price=29.99;
  72.    
  73.     //INSERT
  74.     $sql="Insert into Products(ID,Category,Description, Price, Image) values ($prodId,'$category','$description',$price,1)";
  75.      $msg=$msg. $sql."<BR>";
  76.     mysqli_query($con, $sql);
  77.     if(mysqli_affected_rows($con)==1){$msg= "Insert success<BR>";}          
  78.     else{$msg=$msg. "Something went wrong";}
  79.    
  80.     //UPDATE
  81.     $description="baseball bat";
  82.     $sql="Update products set Description='$description' where ID=$prodId";
  83.    
  84.     mysqli_query($con, $sql);
  85.     $msg=$msg. (mysqli_affected_rows($con)==1) ? "Updated<BR>":"Not Updated<BR>";
  86.    
  87.     //DELETE
  88.     //$sql="delete from products where ID=$prodId";
  89.     //mysqli_query($con, $sql);
  90.     //echo (mysqli_affected_rows($con)==1) ? "Deleted<BR>":"Not Deleted<BR>";
  91.    
  92.  
  93.    
  94. }//end BIG IF statement
  95. //a header redirect will send the user to another page
  96.  
  97. header("location:Chap27.php?message=$msg");
  98. //this is important for sprint 1.
  99.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement