Hey, please help me to fix my code! Fast please!

Discussion in 'PHP' started by Hello 2000, Feb 3, 2014.

  1. #1
    Fix my code, please!

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\


    index.php
    <?php
    include 'includes/connection.php';
    
    $query = "SELECT * FROM tuotteet";
    
    $result = mysql_query($query);
    
    while($tuote = mysql_fetch_array($result)) {
    echo "<h3>" . $tuote['Nimi'] . "</h3>";
    echo "<p>" . $tuote['Kuvaus'] . "</p>";
    echo "<a href=\"modify.php\id=" . $tuote['ID'] . "\">Muokkaa Tuotetta</a>";
    echo "<span> </span>";
    echo "<a href=\"delete.php?id=" . $tuote['ID'] . "\">Muokkaa Tuotetta</a>";
    }
    ?>
    <h1>Lisää tuote</h1>
    <form action="create.php" method="post">
    Tuotteen nimi
    <input type="text" name="inputName" value="" /><br />
    Tuotteen kuvaus
    <input type="text" name="inputDesc" value="" />
    <br />
    <input name="submit" type="submit" id="submit" />
    </form>
    Code (markup):
     
    Last edited by a moderator: Feb 3, 2014
    Hello 2000, Feb 3, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I'd guess that your connection hasn't worked and therefore $result is null.

    This kind of query is deprecated, there are newer, safer ways to do it. Check php.net for options.
     
    sarahk, Feb 3, 2014 IP
  3. Hello 2000

    Hello 2000 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    thx, probly. Damn, i spended alot of time to doing this!

    *for

    *makin

    XD, now lets makin new version of it
     
    Last edited by a moderator: Feb 3, 2014
    Hello 2000, Feb 3, 2014 IP
  4. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    Add a PHP variable which establishes connection with your database. Once done, insert the same variable in the mysql_query() function but before the $query varable.

    For e.g:
    
    $con = mysqli_connect('host', 'username', 'password', 'database');
    
    Code (markup):
    Now add thw $con variable in the mysql_query() function but before the $query parameter.

    For e.g:
    
    $result = mysql_query($con, $query);
    
    Code (markup):
    Just to make you abreast of the latest developments, the 'mysql' extension has been deprecated and it's now been replaced by 'mysqli'. So make sure you update your code with it.
     
    saadi123, Feb 4, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Just an observation, but why are you still using mysql_ functions like it's a decade ago, ignoring the big red warning boxes that they've had for just about two years now... why are you using multiple echo statements to do the job of one? Why do you have a H1 in your markup AFTER the H3? (can I at least assume there's both a H1 and H2 preceeding that?) Why don't you have a complete form with LABELs and at least one FIELDSET? Why are you using double quotes and string addition?

    Of course, I probably wouldn't waste time making the extra variable either... and if VALUE is empty you don't need to say VALUE.

    If I was writing the same thing, it would probably look more like this:
    <?php
    include('includes/connection.php');
     
    $result = mysql_query('SELECT * FROM tuotteet');
    
    while ($tuote = mysql_fetch_array($result)) echo '
    	<h3>', $tuote['Nimi'], '</h3>
    	<p>', $tuote['Kuvaus'], '</p>
    	<a href="modify.php?id=', $tuote['ID'], '">Muokkaa Tuotetta</a>
    	<span> </span><!-- suspect this span is doing padding's job -->
    	<a href="delete.php?id=', $tuote['ID'], '">Muokkaa Tuotetta</a>';
    	
    echo '
    	<form action="create.php" method="post">
    		<h2>Lisää tuote</h2>
    		<fieldset>
    			<label for="createInputName">Tuotteen nimi</label>
    			<input type="text" name="inputName" id="createInputName"/>
    			<br />
    			<label for="createInputDesc">Tuotteen kuvaus</label>
    			<input type="text" name="inputDesc" id="createInputDesc" />
    			<br />
    			<input name="submit" type="submit" id="submit" />
    		</fieldset>
    	</form>';
    	
    ?>
    Code (markup):
    Though I'm guessing on the heading level without seeing the whole page... remember, H1 is the element under which every subsection of the page that FOLLOWS is... well, a subsection, which is why it should be the FIRST numbered heading on a page; and there should only be one of them -- preferably before you have ANY other content. H2's are the start of subsections of the H1, h3's are the start of subsections of the H2 immediately before them, and so forth. :D
     
    deathshadow, Feb 5, 2014 IP
  6. Hello 2000

    Hello 2000 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    I created another one!
     
    Hello 2000, Feb 6, 2014 IP
  7. Hello 2000

    Hello 2000 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    This time i have a stupid job for you, please give me css code to this black box left

    its just left and it looks like that permament still box on pic XD

    fast please
     

    Attached Files:

    Hello 2000, Feb 6, 2014 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #8
    I think you've missed the point that this is a discussion forum.

    You show us how far you have got and we suggest fixes etc

    You know the old thing about teaching a man to fish...
     
    sarahk, Feb 6, 2014 IP
    ryan_uk and malky66 like this.
  9. Hello 2000

    Hello 2000 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    Thx ---> deathshadow your code works, but i already created new one :D. Please can someone post that nice box css code please XD
     
    Hello 2000, Feb 6, 2014 IP
  10. Hello 2000

    Hello 2000 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    Created, but if you could, can you post your version of that box
     
    Hello 2000, Feb 6, 2014 IP
  11. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #11
    Demanding that people help you is not the way to go about it, go away and come back when you've learnt some manners and maybe people here will be more inclined to help you.
     
    malky66, Feb 6, 2014 IP
    ryan_uk likes this.