Jump to content

Html

Members
  • Posts

    640
  • Joined

  • Last visited

Everything posted by Html

  1. <?php # Access session. session_start() ; # Connect to the database. require ('connect_db.php'); function deleteRecord(mysqli $dbc, $user_id){ $sql ="DELETE FROM 'users' WHERE id ='".$user_id."'"; $result = $db->query($sql); if(!result){ throw new Exception('Cannot delete' ); } } ?> <?php # Access session. session_start() ; # Connect to the database. require ('connect_db.php'); function deleteRecord( $dbc ) { $q = 'SELECT * FROM users' WHERE id ='".$user_id."'"; $r = mysqli_query( $dbc , $q ) ; $num = mysqli_num_rows( $r ) ; if ( $num > 0 ) { ?> Which one is correct to use? $user_id is fine instead of $id. The second one has this error, the top code has no errors according to php checker. Error: There are 2 more opening curly braces '{' found This count is unaware if curly braces are inside of a string PHP Syntax Check: Parse error: syntax error, unexpected 'WHERE' (T_STRING) in your code on line 9 $q = 'SELECT * FROM users' WHERE id ='".$user_id."'";
  2. Sure, I get what you've stated, I've shown that n the code example which doesn't do anything. Considering the code is setup with this $q variable, I guess it requires it in the code, as other examples from the book's examples files include this. function deleteRecord( $dbc ) { $q = 'SELECT * FROM users' WHERE id ='".$user_id."'"; $r = mysqli_query( $dbc , $q ) ; $num = mysqli_num_rows( $r ) ; if ( $num > 0 ) { Before I thought about this^ This was what I was using, and that obviously didn't work. <?php # Connect to the database. require ('connect_db.php'); # Access session. session_start() ; function deleteRecord(mysqli $dbc, $user_id){ $sql ="DELETE FROM 'users' WHERE id ='".$user_id."'"; $result = $db->query($sql); if(!result){ throw new Exception('Cannot delete' ); } } ?> Well yes the session needs to be at the top.
  3. I never did get a response on how to remove the data from a db from the author of the example code. That was disappointing. I even went through some php 5 example code from one of their books, on the site, there is only this. <!-- example for PHP 5.0.0 final release --> <?php function delete( $file ) { if( unlink( $file ) ) { echo( "$file<br>has been deleted<hr>" ); } else { echo( "Unable to delete $file<hr>" ); } } ?> Every where has a different way of removing data. I am trying to remove a stored user on the db, so the above code doesn't quite do that, or in my first post. I am not sure if $sql and $q is necessary in those line $r = mysqli_query( $dbc , $sql, $q ) ; remove.php should be fine remove.php?id=1 😦
  4. Hi, If you have remove.php or remove.php?=recordid ?> or I've seen this in numerous clips, some also include $sql $sql = 'SELECT * FROM users' WHERE id ='".$"'"; $r = mysqli_query( $dbc , $sql ) ; Is the above even needed in a function, I read in the php manual, it refers to the rows
  5. I am missing the speech marks from "user_id". As for these exercises, didn't learn anything from them. Show answer just reveals what goes where. I did how ever look at the Delete one, but firefox doesn't support the Websql. But that didn't help me at all.
  6. I uncertain what to do now, I thought with the shop code it would be similar to what I want. On the example here at W3 schools it is "Customers"; https://www.w3schools.com/sql/sql_where.asp
  7. That part alone isn't enough to display a comment from the forum table where id is. { $q .= $id . ','; } that part is useless to that SQL line?
  8. # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id "="$id" I viewed the Where clause on the site here, if that above isn't it. $q = "SELECT * FROM forum WHERE user_id ";
  9. # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id IN (" ; { $q .= $id . ','; }) Well if $q . =$id works for that code then it should be okay for the task I need it for. # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id IN " ; { $q .= $id . ','; } This is fine with no syntax problems, but would this work.
  10. Right okay, I've read what you stated. The reason I copied that was yes I thought it would lead to my object, and second the whole thing is setup a particular way. # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id IN (" ; foreach ($_SESSION['user_id'] as $id =>$id => $value) { $q .= $id . ','; } $r = mysqli_query( $dbc, $q ) ; if ( mysqli_num_rows( $r ) > 0 ) { echo '<table><tr><th>Posted</th><th>Subject</th><th id="msg">Message</th></tr>'; while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { echo '<tr><td>' . $row['first_name'] .' '. $row['last_name'] . '<br>'. $row['post_date'].'</td> <td>' . $row['subject'] . '</td><td>' . $row['message'] . '</td> </tr>'; } echo '</table>' ; } else { echo '<p>There are currently no messages.</p>' ; } This part of the code is where the problem is, and this below is the problem # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id IN (" ; foreach ($_SESSION['user_id'] as $id =>$id => $value) { $q .= $id . ','; }
  11. <?php # DISPLAY COMPLETE FORUM PAGE. # Access session. session_start() ; # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Set page title and display header section. $page_title = 'Forum' ; include ( 'includes/header.html' ) ; # Open database connection. require ( 'connect_db.php' ) ; # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id IN (" ; foreach ($_SESSION['user_id'] as $id =>$id => $value) { $q .= $id . ','; } $r = mysqli_query( $dbc, $q ) ; if ( mysqli_num_rows( $r ) > 0 ) { echo '<table><tr><th>Posted</th><th>Subject</th><th id="msg">Message</th></tr>'; while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { echo '<tr><td>' . $row['first_name'] .' '. $row['last_name'] . '<br>'. $row['post_date'].'</td> <td>' . $row['subject'] . '</td><td>' . $row['message'] . '</td> </tr>'; } echo '</table>' ; } else { echo '<p>There are currently no messages.</p>' ; } # Create navigation links. echo '<p><a href="post.php">Post Message</a> | <a href="shop.php">Shop</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ; # Close database connection. mysqli_close( $dbc ) ; # Display footer section. include ( 'includes/footer.html' ) ; ?> Okay so I've removed the get id code snippet. That isn't useful here.
  12. I just thought it may be required. I have never displayed anything from the db that is related to a user. I tried this with one user on here back in January, but it didn't work out. So having viewed the code from the shop/ cart files, I am comparing and seeing what there is there, that is displayed from a db, to get the idea on how to display a comment posted by the logged in user onto their profile index, which is public, but linked from within the home.php once logged in, so profile link goes to index of profile and displays posted comment. But if you try the login system you can see for yourself how the whole thing is from the book. So forum will be a profile index, which is accessble by any user, but id for only that user, so as it stands now, the forum is public, so post comment and a subject line, the user's first and second name display all in one page, so I want that per user only.
  13. this # Get passed product id and assign it to a variable. if ( isset( $_GET['id'] ) ) $id = $_GET['user_id'] ; I took this from that file, not sure if that has any use for it. But need a get iD? You can try yourself Justsomeguy, if you register on the login system aboringsocialnetworksite.epizy.com You get my point on what I'm trying to do with this.
  14. <?php # DISPLAY COMPLETE FORUM PAGE. # Access session. session_start() ; # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Set page title and display header section. $page_title = 'Forum' ; include ( 'includes/header.html' ) ; # Get passed product id and assign it to a variable. if ( isset( $_GET['id'] ) ) $id = $_GET['user_id'] ; # Open database connection. require ( 'connect_db.php' ) ; # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id IN (" ; foreach ($_SESSION['user_id'] as $id =>$id => $value) { $q .= $id . ','; } $r = mysqli_query( $dbc, $q ) ; if ( mysqli_num_rows( $r ) > 0 ) { echo '<table><tr><th>Posted</th><th>Subject</th><th id="msg">Message</th></tr>'; while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { echo '<tr><td>' . $row['first_name'] .' '. $row['last_name'] . '<br>'. $row['post_date'].'</td> <td>' . $row['subject'] . '</td><td>' . $row['message'] . '</td> </tr>'; } echo '</table>' ; } else { echo '<p>There are currently no messages.</p>' ; } # Create navigation links. echo '<p><a href="post.php">Post Message</a> | <a href="shop.php">Shop</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ; # Close database connection. mysqli_close( $dbc ) ; # Display footer section. include ( 'includes/footer.html' ) ; ?> This example is the forum.php but modified to display only the logged in user's comment on a profile page, if you see, I am just using the example from another code file. I took it from the shop/cart file. So how do I get the comment posted on a public forum, so any user can view to just get only their comment viewable, so forum.php becomes a profile.php page id= thing. <?php # DISPLAY SHOPPING CART PAGE. # Access session. session_start() ; # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Set page title and display header section. $page_title = 'Cart' ; include ( 'includes/header.html' ) ; # Check if form has been submitted for update. if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { # Update changed quantity field values. foreach ( $_POST['qty'] as $item_id => $item_qty ) { # Ensure values are integers. $id = (int) $item_id; $qty = (int) $item_qty; # Change quantity or delete if zero. if ( $qty == 0 ) { unset ($_SESSION['cart'][$id]); } elseif ( $qty > 0 ) { $_SESSION['cart'][$id]['quantity'] = $qty; } } } # Initialize grand total variable. $total = 0; # Display the cart if not empty. if (!empty($_SESSION['cart'])) { # Connect to the database. require ('../connect_db.php'); # Retrieve all items in the cart from the 'shop' database table. $q = "SELECT * FROM shop WHERE item_id IN ("; foreach ($_SESSION['cart'] as $id => $value) { $q .= $id . ','; } $q = substr( $q, 0, -1 ) . ') ORDER BY item_id ASC'; $r = mysqli_query ($dbc, $q); # Display body section with a form and a table. echo '<form action="cart.php" method="post"><table><tr><th colspan="5">Items in your cart</th></tr><tr>'; while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { # Calculate sub-totals and grand total. $subtotal = $_SESSION['cart'][$row['item_id']]['quantity'] * $_SESSION['cart'][$row['item_id']]['price']; $total += $subtotal; # Display the row/s: echo "<tr> <td>{$row['item_name']}</td> <td>{$row['item_desc']}</td> <td><input type=\"text\" size=\"3\" name=\"qty[{$row['item_id']}]\" value=\"{$_SESSION['cart'][$row['item_id']]['quantity']}\"></td> <td>@ {$row['item_price']} = </td> <td>".number_format ($subtotal, 2)."</td></tr>"; } # Close the database connection. mysqli_close($dbc); # Display the total. echo ' <tr><td colspan="5" style="text-align:right">Total = '.number_format($total,2).'</td></tr></table><input type="submit" name="submit" value="Update My Cart"></form>'; } else # Or display a message. { echo '<p>Your cart is currently empty.</p>' ; } # Create navigation links. echo '<p><a href="shop.php">Shop</a> | <a href="checkout.php?total='.$total.'">Checkout</a> | <a href="forum.php">Forum</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ; # Display footer section. include ( 'includes/footer.html' ) ; ?>
  15. Well, I simply want to get this code working out here. This is what I have, and it works, for now it will do, I don't want to be buying another book. So far I got this example code without having to buy another book, which is out of date as already stated. Also the ability to change password would be another feature, so upload one image to a user's profile, display forum data individually to a profile index, I tried messing with some code the other day by using a GET id code, that didn't do anything. ?php # DISPLAY SHOPPING CART ADDITIONS PAGE. # Access session. session_start() ; # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Set page title and display header section. $page_title = 'Cart Addition' ; include ( 'includes/header.html' ) ; # Get passed product id and assign it to a variable. if ( isset( $_GET['id'] ) ) $id = $_GET['id'] ; # Open database connection. require ( 'connect_db.php' ) ; # Retrieve selective item data from 'shop' database table. $q = "SELECT * FROM shop WHERE item_id = $id" ; $r = mysqli_query( $dbc, $q ) ; if ( mysqli_num_rows( $r ) == 1 ) { $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ); # Check if cart already contains one of this product id. if ( isset( $_SESSION['cart'][$id] ) ) { # Add one more of this product. $_SESSION['cart'][$id]['quantity']++; echo '<p>Another '.$row["item_name"].' has been added to your cart</p>'; } else { # Or add one of this product to the cart. $_SESSION['cart'][$id]= array ( 'quantity' => 1, 'price' => $row['item_price'] ) ; echo '<p>A '.$row["item_name"].' has been added to your cart</p>' ; } } # Close database connection. mysqli_close($dbc); That is really for another time. Here is what I put together. What we tried to do was use new code to add it to this, when really it needs to be put a certain way. $q what ever, and all rest of it. code for user id comment posted to a user's index. <?php # DISPLAY COMPLETE FORUM PAGE. # Access session. session_start() ; # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Set page title and display header section. $page_title = 'Forum' ; include ( 'includes/header.html' ) ; # Get passed product id and assign it to a variable. if ( isset( $_GET['id'] ) ) $id = $_GET['user_id'] ; # Open database connection. require ( 'connect_db.php' ) ; # Display body section, retrieving from 'forum' database table. $q = "SELECT * FROM forum WHERE user_id IN (" ; foreach ($_SESSION['user_id'] as $id =>$id => $value) { $q .= $id . ','; } $r = mysqli_query( $dbc, $q ) ; if ( mysqli_num_rows( $r ) > 0 ) { echo '<table><tr><th>Posted</th><th>Subject</th><th id="msg">Message</th></tr>'; while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { echo '<tr><td>' . $row['first_name'] .' '. $row['last_name'] . '<br>'. $row['post_date'].'</td> <td>' . $row['subject'] . '</td><td>' . $row['message'] . '</td> </tr>'; } echo '</table>' ; } else { echo '<p>There are currently no messages.</p>' ; } # Create navigation links. echo '<p><a href="post.php">Post Message</a> | <a href="shop.php">Shop</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ; # Close database connection. mysqli_close( $dbc ) ; # Display footer section. include ( 'includes/footer.html' ) ; ?>
  16. Yes, the code above sure, published in 2016 that ineasysteps book php 7. I sent a message via their site, hopefully to add to the Php & Mysql code examples of the book first published in 2012, to add remove data, I even sent my example code to them so the work is done, who ever just needs to try and test it, and sort it out. Before GDPR, a user site should still have a feature to remove data if somebody no longer wants to be registered. Thanks to the user on here who simply found the code on their site, and I tried out the login system last year. I tried with one user on here to get it to work for a profile, and he got fed up with it, as it was setup a particular way, so I am trying to do exactly as shown in the example files, to get it working with new features. I am just bored with it all. Learning gently php was the book you suggested by O'Reilly. Ineasysteps is good for a beginner, but it is bare bones, which if it worked they I wanted, I wouldn't be posting here.
  17. Php 7 has the upload code, here is the original. <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP File Upload</title> </head> <body> <form method="POST" action="<?php $_SERVER[ 'PHP_SELF' ] ?>" enctype="multipart/form-data"> Select an image to upload : <input type="file" name="image" > <input type="submit" value="Upload Image" > </form> <?php if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) { $name = $_FILES[ 'image' ][ 'name' ] ; $temp = $_FILES[ 'image' ][ 'tmp_name' ] ; $size = $_FILES[ 'image' ][ 'size' ] ; $ext = pathinfo( $name , PATHINFO_EXTENSION ) ; $ext = strtolower( $ext ) ; if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' ) { echo 'Format must be PNG, JPG, or GIF' ; exit() ; } if( $size > 512000) { echo 'File size must not exceed 500Kb' ; exit() ; } if( file_exists( $name ) ) { echo 'File '.$name.' already uploaded' ; exit() ; } try { move_uploaded_file( $temp , $name ) ; echo 'File uploaded : '.$name ; echo '<br><img src="'.$name.'">' ; } catch( Exception $e ) { echo 'File upload failed!' ; } } ?> </body> </html> And the other book you suggested, which I bought on Amazon in may 2018.
  18. The books I have are, Php 7 by ineasysteps, which according to Justsomeguy is garbage now. And the is Php in basic steps or something now, it is php 7, a lot more complicated. The ineasysteps are basic and not very informative. As for the code, I am using example code from the php 7 book, which is an upload feature, it works as is, as is obvious I am trying to tailor it to a user who is logged in and wants to upload an image to their profile page, which for now would be the home.php would just include an avatar like on this forum. And a number of libraries have closed, these books are dirt cheap. Recycling isn't a bad thing, not sure how much of an environmental friendly way anything is broken down, paper is recyclable
  19. I already put the braces in, then it didn't change anything. And I sent an email message via the ineasysteps site to try and get some code added to the example files, like trying to remove data, that is something the book that they published in 2012, i guess, with php 5 code I am using never contained. May be somebody wants to remove their account, strange how they didn't about that. As for the php 7 book from ineasysteps, which is probably out of date code and (only had that forum code the only useful bit, and some contact page code, that never got working on the old login files,) I haven't checked it out in a while, I'm recycling it, along with the other book. They remain in a bag with a number of old cd, electronics to go the recycling center, I don't have a use for them. But sure, I want to try and get this login system working a particular way with some features, I haven't given up entirely yet on this. Sure it was about two years ago I first tried a youtube clip, the guy who made the example files, never responded back via email, and just made a few clips explaining a login system, that did nothing compared to the one from Php & Mysql book.
  20. I don't know, I tried this again, I removed the braces above, not suppose to do that, I keep now getting a catch error. <?php # Access session. session_start() ; # Open database connection. require ( 'connect_db.php' ) ; # DISPLAY COMPLETE LOGGED IN PAGE. # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Retrieve selective item data from 'images' database table. $q = "INSERT * INTO images WHERE id = $id" ; $r = mysqli_query( $dbc, $q ) ; if ( mysqli_num_rows( $r ) == 1 ) $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ); if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) $name = $_FILES[ 'image' ][ 'name' ] ; $temp = $_FILES[ 'image' ][ 'tmp_name' ] ; $size = $_FILES[ 'image' ][ 'size' ] ; $ext = pathinfo( $name , PATHINFO_EXTENSION ) ; $ext = strtolower( $ext ) ; if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' ) { echo 'Format must be PNG, JPG, or GIF' ; exit() ; } if( $size > 512000) { echo 'File size must not exceed 500Kb' ; exit() ; } if( file_exists( $name ) ) { echo 'File '.$name.' already uploaded' ; exit() ; } try move_uploaded_file( $temp , $name ) ; echo 'File uploaded : '.$name ; echo '<br><img src="'.$name.'">' ; catch ( Exception $e ) echo 'File upload failed!' ; # Close database connection. mysqli_close( $dbc ) ; ?>
  21. The code above will prevent anyone from accessing upload.php same when logging into home.php has the same code. Otherwise upload.php can be accessed by any visitor. It just redirects to login, so one has to register to use any features of the site. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } That is the correct code for the top. try move_uploaded_file($temp, $name); echo 'File uploaded : ' . $name; echo '<br><img src="' . $name . '">'; } { That is missing above move.
  22. Okay, so I need { one of those in a few places in the code. # Redirect if not logged in. if (!isset($_SESSION['user_id'])) { require('login_tools.php'); load() # Retrieve selective item data from 'images' database table. $q = "SELECT * FROM images WHERE id = $id"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array($r, MYSQLI_ASSOC); if ($_SERVER['REQUEST_METHOD'] == 'POST') $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $size = $_FILES['image']['size']; $ext = pathinfo($name, PATHINFO_EXTENSION); $ext = strtolower($ext); if ($ext != 'png' && $ext != 'jpg' && $ext != 'gif') { echo 'Format must be PNG, JPG, or GIF'; exit(); } if ($size > 512000) { echo 'File size must not exceed 500Kb'; exit(); } if (file_exists($name)) { echo 'File ' . $name . ' already uploaded'; exit(); } try move_uploaded_file($temp, $name); echo 'File uploaded : ' . $name; echo '<br><img src="' . $name . '">'; } catch(Exception $e ) echo 'File upload failed!'; } The problem is here.
  23. No, I want to store the image uploaded into the db from any user who uploads. 'INSERT INTO images where id' is is the sql I need. Like I stated I took snippets from one file and have added them into the basic upload code as I think it would obviously need them to work with as the other files contain the similar consistent code.
  24. # Retrieve selective item data from 'images' database table. $q = "SELECT * FROM images WHERE id = $id"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array($r, MYSQLI_ASSOC); The opening curly is there, that is what you are on about. There needs to be two in this block. If statement, like the others below. So under $row line a closing one is needed.
  25. Sure, I paste the whole code. I would do that, it wouldn't make sense to just post individual snippets.
×
×
  • Create New...