Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

whats wrong?!?!?!?!

Codes here !

Moderators: macek, egami, gesf

whats wrong?!?!?!?!

Postby tranquillo » Sun Jan 05, 2003 7:45 pm

:evil: damnit! :evil:
now I'm getting pissed.

am I just to dumb to get this or what?

I've been to many different tutorials and I never get this thing working... I just want to enter some data into a database, how hard can it be???

I posted a topic a while ago with some php code that didn't work... and now I've been looking at some other solutions to the problem... a few look a lot like and this is one of them..

this is inserted to a html page:
--------------------------------
<form name="links" method="post" action="add_links.php">
<p>Title :
<input type="text" name="title">
<br>
<br>
URL :
<input type="text" name="url">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p></form>
--------------------------------
and is linked to add_links.php
add_links.php looks like this:
--------------------------------
<?php
$db = mysql_connect("localhost","user","password");
mysql_select_db (database name);
$query = "INSERT INTO links(title , url)
VALUES('$title','$url')";
$result = mysql_query($query);
echo ("Link entered.");
?>
--------------------------------
now, what happens is that the database is updated (wich is a step in the right direction). but only the $id is updated... nothing else is entered into the database... I had a similar code and the same thing happened there.. only the id was entered...
--------------------------------
the table "links":
(
id mediumint(10) NOT NULL auto_increment,
title varchar(80),
url varchar(80),
PRIMARY KEY (id)
)
--------------------------------
and if you wanna know, I'm not just looking to steal some code, I wanna know how it works.

thanks
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Mon Jan 06, 2003 11:46 am

mmmm,
have a look at the other answer, maybe it has something to do with it...
http://www.php-forum.com/p/viewtopic.php?t=1005

If not, just post back.
bye!
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby tranquillo » Mon Jan 06, 2003 2:21 pm

after compareing a thousend codes I finaly got it working.. :) thanks...

the way I solved it is that I use a part of the code 10 times to get the 10 latest inputs out of the database. is there a way to do that without copying the code...

printf("<b>First Name:</b> %s<br>\n", mysql_result($result2,0,"namn"));
printf("<b>e-mail adress:</b> %s<br>\n", mysql_result($result2,0,"epost"));
printf("<b>Message</b>: %s<br>\n", mysql_result($result2,0,"txt"));

this is the code I copy and just change "($result2,0,"txt")" to "($result2,1,"txt")" and so on...

and another thing, for another thing...
I'm thinking of using php on other places on the site... and there's a list that I'd like to have every second line in a different color... is that possible to do at one place or do I have to make up a table and set the color on everyone... it should be first a white background, then a grey, then another white and then another grey. and so on... and here I really would need to have one code to list all entrys because the number of entrys will change...

I'm feeling like a real pain in the ***...


thanks ;)
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Mon Jan 06, 2003 4:52 pm

With this code you see if a number is odd or even:
Code: Select all
echo ($var%2) ? "Odd!" : "Even!";


You can use it to assign two different colors when you output an array from a Mysql query.

Code: Select all
i=0;
while ( $row = mysql_fetch_array ( $result ) ){
   echo ($var%2) ? "color1" : "color2";
   echo $row[0];
}


bye :)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby tranquillo » Wed Jan 08, 2003 9:16 am

hi...

I don't seem to get any " mysql_fetch_array " tags working. I've tried before and I just dont get it... could you explain how it works?

thanks.
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby tranquillo » Wed Jan 08, 2003 9:36 am

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpdev\www\public\databas\testcolor.htm on line 26

line 26:
while ( $row = mysql_fetch_array ($result)){
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Wed Jan 08, 2003 3:14 pm

Post the piece of code where the Mysql query is, so we can have a look :)

Anyway:
$result is the variable that contents the query and you use mysql_fetch_array to itinerate through all rows. Then use echo $row[0]; (or $row["my_row_name"]; ) to get the values of the named row/s.

Code: Select all
while ( $row = mysql_fetch_array ( $result ) ){
   echo $row["name"]."<br>";
   echo $row["email"]."<br>";
   echo $row["website"]."<p>";
}
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby tranquillo » Thu Jan 09, 2003 6:46 am

<?php

$db = mysql_connect("localhost", "root");
mysql_select_db("seko",$db);

$sql = "SELECT * FROM arbetsgivare";

$result = mysql_query($sql);

while ( $row = mysql_fetch_array ($result)){
echo ($var%2) ? "#999999" : "#333333";
echo $row[0];
}

?>

this is the whole thing...


so if I do
echo $row["name"]."<br>";
echo $row["email"]."<br>";
echo $row["website"]."<p>";
if there's 5 rows they all will show. right?

name1
email1
website1

name2
email2

and so on... right?

and the color thing... the:
i=0;
while ( $row = mysql_fetch_array ( $result ) ){
echo ($var%2) ? "color1" : "color2";
echo $row[0];
}
does it color the text or the background?
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Thu Jan 09, 2003 10:19 am

This is a example, use it as you want:
Code: Select all
$var=0;
while ($row = mysql_fetch_array ($result)) {
   echo ($var%2) ? "#999999<br>" : "#333333<br>";
   $var++;
   
}


bye!
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby tranquillo » Fri Jan 10, 2003 2:46 pm

thank you sooo much... :)
took me a while to get the colors into the bgcolor tags and get the tables to look good... but now it's just perfect. just the way I wanted it...

I love PHP
I love MySQL
I love this place
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Fri Jan 10, 2003 3:33 pm

Me too,
I love it all
:D
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby tranquillo » Sun Jan 12, 2003 8:07 pm

here we go again.. ;)

----------------------------
----------------------------

<?php

$db = mysql_connect("localhost", "root");
mysql_select_db("seko",$db);

$kid = $_POST["kid"];
$namn = $_POST["namn"];
$gadr = $_POST["gadr"];
$postnr = $_POST["postnr"];
$ort = $_POST["ort"];
$sek = $_POST["sek"];

if ($Submit) {

$query = "INSERT INTO klubb(klubbID,namn,gatuadress,postnummer,ort,sektion)
VALUES('$kid','$namn','$gadr','$postnr','$ort','$sek')";
$result = mysql_query($query);

$result2 = mysql_query("SELECT * FROM klubb order by klubbID desc",$db);
}
if ($myrow = mysql_fetch_array($result2))
{
printf("<b>Klubb ID:</b> %s<br>\n", mysql_result($result2,0,"klubbID"));
printf("<b>Klubbnamn:</b> %s<br>\n", mysql_result($result2,0,"namn"));
printf("<b>Adress:</b> %s<br>\n", mysql_result($result2,0,"gatuadress"));
printf("<b>Postnummer:</b> %s<br>\n", mysql_result($result2,0,"postnummer"));
printf("<b>Ort:</b> %s<br>\n", mysql_result($result2,0,"ort"));
printf("<b>Sektion:</b> %s<br>\n", mysql_result($result2,0,"sektion"));
} else {
echo "Sorry, no records were found!";
}

?>

----------------------------
----------------------------

<form name="links" method="post" action="<?php echo $PHP_SELF?>">
<p><br>
<table border="0" class="textsmallbold">
<tr>
<td> Klubb ID : </td>
<td><input type="text" name="kid"></td>
</tr>
<tr>
<td>Klubbens namn : </td>
<td><input type="text" name="namn"></td>
</tr>
<tr>
<td>Gatuadress : </td>
<td><input type="text" name="gadr"></td>
</tr>
<tr>
<td>Postnummer : </td>
<td>Ort :</td>
</tr>
<tr>
<td><input type="text" name="postnr">
<td><input type="text" name="ort"></td></td>
</tr>
<tr>
<td>Sektion : </td>
<td> <select name="sek">
<option value="V&auml;g&amp;Ban sydost">V&auml;g&amp;Ban sydost</option>
<option value="V&auml;g&amp;Ban nordost">V&auml;g&amp;Ban nordost</option>
</select></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr>
<td colspan="2" align="left">&nbsp;</td>
</tr>
</table></p>
</form>

----------------------------
----------------------------

what am I doing wrong? this code used to work... but I moved it around and at the beginning it was split up on two pages... and now it don't work... is it the --- action="<?php echo $PHP_SELF?>"> --- thing that screws thing up? coz that's the only thing I can think of... (it's 5am, so I might have missed a couple of things though) and I don't know how to do the php_self thing...

thanks
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Mon Jan 13, 2003 3:29 am

$PHP_SELF should work,
have you tried to change this statement for the URL of the page in the html way??

Anyway,
which error do you get??
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby tranquillo » Mon Jan 13, 2003 4:25 pm

I worked it out... I think it was the if ($Submit) { part that was tha problem I solved it by making it if ($kid) { instead...


at least I think that's it... :) anyways, it works now...

thanks for the help

-s
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron