Jump to content
Html

Upload image code trying to add db connect to it

Recommended Posts

You will never be able to fix this until you understand the language. You should go through the full PHP tutorial from the beginning.

It was mentioned before that the try statement should have brackets, and also that the try statement is useless in this context.

Share this post


Link to post
Share on other sites

Yeah, there's nothing more that I can add.  You have several books about PHP but you've never started at the beginning, you always try to jump ahead to something that you're trying to do.  Maybe after a couple years of trying and failing you'll realize that doing that was not a good idea.  There's nothing else to add, if you don't understand the basics you won't be able to do anything, and I don't understand your reluctance to just read from the beginning and make sure you understand everything.  I really hope it's not an issue of you not wanting to spend the time to do that, because you have wasted a tremendous amount of time already trying to do things that you never bothered to learn how to do.  If I point out basic errors five times and you still don't understand, then I'm not helping, and I can't help if you don't know what I'm talking about in the first place.

Share this post


Link to post
Share on other sites

If you have long enough to copy and paste every bit of similar code, you might well get it to work! Similar to given monkeys using typewriter a long enough time, they might come up with hamlet.

Share this post


Link to post
Share on other sites

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.

Edited by Html

Share this post


Link to post
Share on other sites
Quote

I already put the braces in, then it didn't change anything.

Then you 1) didn't do it correctly, or 2) didn't notice a different basic syntax error.  I pointed out multiple syntax errors, and the presence of any one of them, again, will cause the entire thing to break.  So, did you fix all of them?  Did you fix the braces correctly?  I don't know, and if you don't know either then that's a problem.  Because in the most recent code you posted, you definitely did not, and for some reason you're still trying to use a try/catch block.  Which I have no idea why.  And I've told you this.

This whole thing is like someone who refuses to learn the basics about the Russian language but still thinks they can call someone who only speaks Russian and ask them what time it is.  I don't know why you think programming is as easy as you think it is, but it's not.  You can't just skip over all of the basics and expect to be able to do anything, I don't know what other skill set is like that and why you think programming is one of them.  What you're trying to do is very easy for someone who knows PHP, but you don't know PHP.  Just like asking a Russian speaker for the time is very easy for someone who knows Russian.

Share this post


Link to post
Share on other sites

Please don't throw the book away, donate it to a library or give it to somebody who needs it. I highly doubt that the book is so outdated that it it useless. Even if the book had some outdated concepts, programming theory doesn't change.

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

I'm not familiar with that specific book, I don't know if I remarked on specific code examples or if I was commenting on the particular code you were posting here.  I don't have an opinion on the specific book.

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

That code is from a book?  Yeah, that's kind of crap, they don't bother to check the error code for the uploaded file and they're trying to use the try/catch block that is pointless.

Which book did I suggest?  I normally only suggest recent O'Reilly books, which I expect to contain decent code given their reputation.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

That O'Reilly book seems pretty good, 400-odd pages is about what I expect for an intro to PHP, and the reviews are generally positive.  There are higher-ranked O'Reilly PHP books, but many of them deal with additional technologies like MySQL and Javascript, not just PHP.  I would stay away from Amazon's #1 book in PHP programming though, which is the Kindle edition of "The Super Easy Sous Vide Cookbook: 575 Best Sous Vide Recipes of All Time".

Share this post


Link to post
Share on other sites

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' ) ;

?>

 

Share this post


Link to post
Share on other sites

What exactly is that code supposed to do?  You're saying that code is for posting a new comment?

Share this post


Link to post
Share on other sites
<?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' ) ;

?>

 

Share this post


Link to post
Share on other sites
 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.

Share this post


Link to post
Share on other sites

I took it from the shop/cart file.

Yeah, I see that.  Why did you do that?  What does adding an item to a shopping cart have to do with displaying a forum?  You've added a PHP syntax error and there's also going to be a SQL syntax error in that code, so none of that is going to run.  Displaying records from a single database table is pretty easy, that's not something complex enough that you need to copy some other piece of unrelated code and try to change it.  The changes don't make sense, you're trying to loop through $_SESSION['user_id'] like it's an array.  Why would that be an array?  That doesn't make sense.  None of this code makes much sense, you're trying to take an unrelated piece of code and change it into something that should be easy to do by itself.

I took this from that file, not sure if that has any use for it. But need a get iD?

I don't know, do you?  You're the programmer, do you need to pass an ID of something in the URL?  Does that accomplish some goal that this page needs?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
<?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.

Share this post


Link to post
Share on other sites

I just thought it may be required.

It's required if you need it, as the programmer.  That's your job to figure out, you're the programmer.

I have never displayed anything from the db that is related to a user.

It doesn't matter that it's a "user", the database does not distinguish between the different "objects" that you try to store in the database.  What the records mean to you does not matter, it does not change how to retrieve them.  Every table in the database holds records, and getting records from a database table is the exact same regardless of what you've decided to name the table or the fields.  You still use a select query.  There's not a particular unique query for "getting users" versus "getting videos" or "getting comments" or whatever else.  They all use the exact same kind of select query.  You're the one that is making the distinction here, not the database.  The database doesn't care what the records mean to you, it does not store things or retrieve things differently just because they have a different meaning to you.  That's why you're not finding specific instructions about "how to add a comment" or "how to add a profile" or whatever else - the actual data does not matter, it does not change the programming.  You're supposed to learn the generic principles about this and then apply it to your own situation, but you've skipped the part about generic principles and instead are trying to find tutorials or videos or whatever that relate directly to your specific situation.

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.

It's simple, and is the exact same way you display any data from the database on any other page.  You use a select query to tell it what data to get, and then you display it.  Do you really think the database cares that the page where you're trying to display the data is linked from "home.php?"  Do you think that matters at all?  Or that it's "public" (whatever that means)?  Or that you're also going to be checking the session to see if someone is logged in?  None of that matters.

Share this post


Link to post
Share on other sites

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 . ','; }

 

Share this post


Link to post
Share on other sites

That's right, you have a syntax error in that PHP code, and I also questioned why you were treating $_SESSION['user_id'] as an array when that doesn't seem like it would be an array.  And, whether or not that foreach loop runs at all, the SQL query is also going to have a syntax error in it.  If that IS an array, and let's say it has 2 values, then you've built a SQL query that looks like this:

SELECT * FROM forum WHERE user_id IN (0, 1

That's not right.  If it's not an array, or an empty array, then this is your SQL query:

SELECT * FROM forum WHERE user_id IN (

That's not right either.

So, yeah, I'm wondering why you're using that code to build a basic select query to select the data you want from the database.  You shouldn't have to copy and paste code to do this, if you understand how select queries work then it should be pretty apparent how to write the query you need to get the data you're looking for.

Share this post


Link to post
Share on other sites
# 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.

Share this post


Link to post
Share on other sites

Well if $q . =$id works for that code then it should be okay for the task I need it for.

What?  If any arbitrary line of code works on a page that displays shopping cart information, then the same line of code belongs in any other page that uses the database?  OK, if you say so.  You're the programmer.

This is fine with no syntax problems, but would this work.

Of course not, look at the SQL query you're trying to build.  It's not a valid SQL query.

Share this post


Link to post
Share on other sites

# 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 ";

Edited by Html
New snippet

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...