PHP, Database, HTML, code problems

Discussion in 'PHP' started by dirtylaundry, Jul 30, 2012.

  1. #1
    hey all, I'm really new to this and this is one of my first projects. I recently just added a new field into my database, the "transfers" and as soon as I updated everything to have the transfers field as an option, the database stop communicating with the input fields. I don't know what happened, I am not collecting any errors... any chance anyone could quite possibly help with this? This problem is located in the UPDATETOTALS.php and LISTTOTALS.php I'm also having issues with deleting information from the database. (which is the DELETETOTALS.php and LISTTOTALS.php) I'm trying to be able to delete the row directly from the table that is printed out from the database. Looking for any help I can find! Thanks! Please be patient with me, I am brand new to this.

    *Edit: I figured out my problem with updatetotals.php... still having trouble deleting*


    View attachment daily totals.txt
     
    Last edited: Jul 30, 2012
    dirtylaundry, Jul 30, 2012 IP
  2. flotwig

    flotwig Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Well, what class are you using to connect to the database? mysql_* functions send errors to mysql_error(), MySQLi and PDO both offer similar functionality. Check the output of those functions and it should give you a decent clue as to what is causing the error.

    Don't forget to quote your values and your column names and such.
     
    flotwig, Jul 30, 2012 IP
  3. D3Tek

    D3Tek Active Member

    Messages:
    164
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    50
    #3
    In your delete.php file, try this:

    mysql_query("DELETE FROM `Totals` WHERE `id` = ".$id) or die(mysql_error());

    Also, are you sure the database table name is "Totals" (with the capital T)?

    The problem was, I think you was getting confused between and apostrophe and a back-tick, the back-tick is for reserved names. I have included a back-tick just because I feel as if that's what you were trying to achieve. Also, there is no need to quote numbers (for example, you used '$id'). It's not going to do ample amounts of damage, but it's recommended that you avoid it, where MySQL will automatically convert it into an int, as it was cast as a string, it still means MySQL is performing an unnecessary typecast/conversion.

    I hope this helps.
     
    D3Tek, Jul 31, 2012 IP
  4. clarkrobin

    clarkrobin Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank for your post!!! I want to know how to use MySql use in php....
     
    clarkrobin, Jul 31, 2012 IP
  5. trungtamthietbi

    trungtamthietbi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This problem is located in the UPDATETOTALS.php and LISTTOTALS.php I'm also having issues with deleting information from the database. (which is the DELETETOTALS.php and LISTTOTALS.php)
     
    trungtamthietbi, Aug 2, 2012 IP
  6. rockycabaero

    rockycabaero Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try to read the contents of this website:
    w3schools.com/sql/default.asp
     
    rockycabaero, Aug 2, 2012 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    from what I'm seeing looks to me like a lot of your errors are just typo's -- forgetting to include the "=" on HTML attributes, missing closing parenthesis, etc, etc... of course the hard to read code thanks to the opening and closing PHP on every line, large number of extra variables that serve no legitimate purpose, oddball willy-nilly indentation style, referencing elements (like $row) you've not even define... cleaning up your coding habits and checking for those types of mistakes would really help you, and the people trying to help you, out. I'd also advise ditching php's deprecated mysql_ functions for mysqli or PDO... there's no sense in your learning the wrong and unsafe way of doing things.

    Your markup is just scary bad -- strong inside TD doing TH's job, attributes that have no business in any markup written after 1998 like BORDER and CELLPADDING, lack of SCOPE, H1 doing CAPTION's job... non-breaking spaces doing CSS' job... no checking if the session value actually exists before trying to access it...

    Let's use just one bit as an example, that 'daily totals' table... the logic AND the markup need serious help.

    
    echo '
    	<table class="dailyTotals">
    		<caption>Daily totals</caption>
    		<thead>
    			<tr>
    				<th scope="col">',(
    					isset($_SESSION['sort']) && ($_SESSION['sort']=='T_name') ?
    					'Branch Name' :
    					'<a href="'.$_SERVER['PHP_SELF'].'?sort=T_name">Branch Name</a>'
    				),'</th>
    				<th scope="col">',(
    					isset($_SESSION['sort']) && ($_SESSION['sort']=='date') ?
    					'Date' :
    					'<a href="'.$_SERVER['PHP_SELF'].'?sort=date">Date</a>'
    				),'</th>
    				<th scope="col">Cash</th>
    				<th scope="col">Checks</th>
    				<th scope="col">Total</th>
    				<th scope="col">Transfers</th>
    				<td></td>
    			</tr>
    		</thead><tbody>';
    
    $query='SELECT * FROM Totals'.(
    	isset($_SESSION['sort']) ?
    	' ORDER BY '.$_SESSION['sort'] :
    	''
    );
    $result = mysql_query($query);
    while ($row=mysql_fetch_assoc($result)) {
    	echo '
    			<tr>
    				<td>',$row['T_name'],'</td>
    				<td>',$row['date'],'</td>
    				<td>',$row['cash'],'</td>
    				<td>',$row['checks'],'</td>
    				<td>',$row['totals'],'</td>
    				<td>',$row['transfers'],'</td>
    				<td>
    					<a href="edittotals.php?id=',$row['id'],'">Edit</a>
    					<a href="deletetotals.php?id=',$row['id'],'">Delete</a>
    				</td>
    			</tr>';
    }
    	
    echo '
    		</tbody>
    	</table>';
    
    Code (markup):
    Is what I'd probably have there -- well, not entirely, since I'd be using PDO.

    Don't make extra variables you don't have to, learn how to leverage echo for cleaner/simpler code WITHOUT going <?php ?> unnecessarily, learn how to properly build the markup BEFORE writing ANY PHP, and, well... slow down a little. I've got the feeling you're just getting ahead of yourself -- moving on to advanced concepts before you have some of the simpler ones down pat.
     
    deathshadow, Aug 3, 2012 IP
  8. trungtamthietbi

    trungtamthietbi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank for your reply!!! I want to know how to use in php like this
     
    trungtamthietbi, Aug 3, 2012 IP