[SOLVED] Connecting to mySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

[SOLVED] Connecting to mySQL

Post by FOsk8r »

Im am trying to make a addnews feature to my site, with a page titled addnews_form.html and a page titled addnews_action.php.
For some reason when the action part isnt connecting to the db, but it is giving me the echo message telling me that it was added, heres the code


addnews_action.php

Code: Select all

<?php
if (($Name == "") || ($Content == ""))
{
	echo "<form name=form method=post action=addnews_action.php>";
	echo "Fill In Required Places<br>";
	echo "You Have Missed Places<br>";
}
if ($Name == "")
{
	echo "<p class=bodymd>Name<br><input type=text name=Name><br><br>Content:<br>$Content</p>";
}
else
{
	echo "<input type=hidden name=Name value=$Name>";
}
if ($Content == "")
{
	echo "<p class=bodymd>Name:<br><b>$Name</b><br><br>Content<br><textarea name=Content rows=10 cols=50></textarea></p>";
}
else
{
	echo "<input type=hidden name=Content value=$Content>";
}
if (($Name == "") || ($Content == ""))
{
	echo "<input type=submit name=Submit value=Submit>";
	echo "<input type=reset name=Reset value=Clear Form>";
	echo "</form>";
}
else
{
	$conn = mysql_connect("localhost","login","12345") or die ("Couldn't connect to DB.");
	$db = mysql_select_db("tp4r03_uk_db"); 

	$result= MYSQL_QUERY("INSERT INTO news (id, name, content)". "VALUES ('NULL', '$name', '$content')");
}
{
	echo "<p class=bodymd><b>$Name</b>, Your New Was Added</p>";
}
?>
Is there anything wrong with it, again it gives me the echo "your news was added", but when I go to PhpMyAdmin there is nothing in the table,
the table.

Any help, please
FOsk8r?
Last edited by FOsk8r on Sun Dec 28, 2003 12:02 pm, edited 3 times in total.
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post by Chambrln »

Try changing the last part to this.

Code: Select all

<?php
if (($Name == "") || ($Content == "")) { 
   echo "<input type=submit name=Submit value=Submit>"; 
   echo "<input type=reset name=Reset value=Clear Form>"; 
   echo "</form>"; 
} else { 
   $conn = mysql_connect("localhost","tp4r03","") or die ("Couldn't connect to DB."); 
   $db = mysql_select_db("tp4r03_uk_db"); 

   if ($result= MYSQL_QUERY("INSERT INTO news (id, name, content) VALUES ('NULL', '$name', '$content')")) {
     echo "<p class=bodymd><b>$Name</b>, Your New Was Added</p>"; 
   } else {
     echo "Didn't update";
   }
}
?>
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

Thanks, now that I added that it says "didnt update"
so what now?
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post by Chambrln »

you might try it with MYSQL_QUERY in lower case. I can't remember if that actually affects it or not. Don't think so though other than that I would have to know your DB layout to know if waht you are inserting into the fields are valid field types and the correct number of fields.
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

heres how my db is stet up,

database - db
table name - news
input fields - id, name, and content

news table

Code: Select all

id---------------- auto integer ---- default 0 ------ primary
name ---------------- text ----------------------------------------
content--------------- text ----------------------------------------
and heres the code again,

addnews_action.php

Code: Select all

<?php 
if (($Name == "") || ($Content == "")) { 
   echo "<input type=submit name=Submit value=Submit>"; 
   echo "<input type=reset name=Reset value=Clear Form>"; 
   echo "</form>"; 
} else { 
   $conn = mysql_connect("host","login","passworkd") or die ("Couldn't connect to DB."); 
   $db = mysql_select_db("tp4r03_uk_db"); 

   if ($result= MYSQL_QUERY("INSERT INTO news (id, name, content) VALUES ('NULL', '$name', '$content')")) { 
     echo "<p class=bodymd><b>$Name</b>, Your New Was Added</p>"; 
   } else { 
     echo "Didn't update"; 
   } 
} 
?>

so your saying that the

Code: Select all

$result=MYSQL_QUERY
should be

Code: Select all

$result=mysql_query
???

thanks alot
FOsk8r?
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post by Chambrln »

Try changing this

Code: Select all

<?php
if ($result= MYSQL_QUERY("INSERT INTO news (id, name, content) VALUES ('NULL', '$name', '$content')")) { 
     echo "<p class=bodymd><b>$Name</b>, Your New Was Added</p>"; 
   } else { 
     echo "Didn't update"; 
   }
?>
to this:

Code: Select all

<?php
if ($result= mysql_query("INSERT INTO news (id, name, content) VALUES ('', '$Name', '$Content')")) { 
     echo "<p class=bodymd><b>$Name</b>, Your New Was Added</p>"; 
   } else { 
     echo "Didn't update"; 
   }
?>
It looks like at the top your variables have capital first letters. So you need to carry that through to the query as well. And for the id since it is autogenerating try it with just '' and let us know if it works.
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

Yeah I changed the MYSQL_QUERY to mysql_query, and changed the vaiables to match the other ones, and now I get an parse error on line 144 and

line 144 reads

Code: Select all

</html>
so I dont know what is wrong
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You are missing an echo/print-ending somewhere above that.

Code: Select all

<?php
echo 'some text here'
</html>
?>
...will get you an error on line 3, alltho the error in real is a missing ; on line 2.

On a sidenote, you should read this post; viewtopic.php?t=511 about passing variables.
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

alright I still cant seem to get it to work, is there maby anyway I can have it write to a .txt file, and save the $Name and the $Content variables so that I can call upon them on a diffrent page?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Connecting to mySQL

Post by infolock »

#1, you can put the mysql_query in upper case, lower case, OR mixed case and it does NOT matter which. So, the fact that it has to be in lower case is just wrong.

#2, you had some wierd ways you were calling through your code, so i cleaned it up.

#3, was $name and $content suposed to be values passed from a different page? IF SO, this should work :

Code: Select all

<?php
if (($_POST['name'] == "") || ($_POST['content'] == ""))
{
	echo "<form name=form method=post action=addnews_action.php>";
	echo "Fill In Required Places<br>";
	echo "You Have Missed Places<br>";
	if ($_POST['name'] == "")
	{
		echo "<p class=bodymd>Name<br><input type=text name=Name><br><br>Content:<br>".$_POST['content']."</p>";
	}
	else
	{
		echo "<input type=hidden name=Name value=".$_POST['name'].">";
	}
	if ($_POST['Content'] == "")
	{
		echo "<p class=bodymd>Name:<br><b>".$_POST['Name']."</b><br><br>Content<br><textarea name=Content rows=10 cols=50></textarea></p>";
	}
	else
	{
		echo "<input type=hidden name=Content value=".$_POST['Content'].">";
	}
	echo "<input type=submit name=Submit value=Submit>";
	echo "<input type=reset name=Reset value=Clear Form>";
	echo "</form>";
exit;
}

$conn = mysql_connect("localhost","login","12345") or die ("Couldn't connect to DB.");
$db = mysql_select_db("db") or die(MySQL_Error());  // change DB to your database name..
$sql= "INSERT INTO news (id, name, content)". "VALUES ('NULL', '".$_POST['name']."', '".$_POST['content']."')";
$result=MySQL_Query($sql) or die(MySQL_Error());
{
	echo "<p class=bodymd><b>".$_POST['Name']."</b>, Your New Was Added</p>";
}
?>


if it wasn't and you just declared the variables and their values before the code you posted, then THIS should work :

Code: Select all

<?php
if (($Name == "") || ($Content == ""))
{
	echo "<form name=form method=post action=addnews_action.php>";
	echo "Fill In Required Places<br>";
	echo "You Have Missed Places<br>";
	if ($Name == "")
	{
		echo "<p class=bodymd>Name<br><input type=text name=Name><br><br>Content:<br>".$Content."</p>";
	}
	else
	{
		echo "<input type=hidden name=Name value=".$Name.">";
	}
	if ($Content == "")
	{
		echo "<p class=bodymd>Name:<br><b>".$Name."</b><br><br>Content<br><textarea name=Content rows=10 cols=50></textarea></p>";
	}
	else
	{
		echo "<input type=hidden name=Content value=".$Content.">";
	}
	echo "<input type=submit name=Submit value=Submit>";
	echo "<input type=reset name=Reset value=Clear Form>";
	echo "</form>";
exit;
}

$conn = mysql_connect("localhost","login","12345") or die ("Couldn't connect to DB.");
$db = mysql_select_db("db") or die(MySQL_Error());  // Change db to your Database Name.
$sql= "INSERT INTO news (id, name, content)". "VALUES ('NULL', '".$Name."', '".$Content."')";
$result=MySQL_Query($sql) or die(MySQL_Error());
{
	echo "<p class=bodymd><b>".$Name."</b>, Your New Was Added</p>";
}
?>

PLEASE NOTE
If you are NOT passing the variables from a nother page and you just defined the variables before the code you posted, YOU MUST use the same casing that you used when you first defined them as they are case-sensative. I found that out the hard way myself.

Anyways, that should help you man. Have fun.
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

Thanks alot, Infolock, yeah I am using a form on page addnews.html to pass the vairables to the addnews_action.php page, so I need to use the post variables as in the first example, now that I have that can I just use on my index page something like this?

Code: Select all

<?php
echo "posted by <b>$Name</b>";
echo "$Content";
?>
then add some html to make it look a little better.

or am I going to somehow open the database and then echo the varables?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

here is the thing. when you are using POST or GET from a page, you should always use $_POST to call them.

Since we can use $_POST to call them, we should just make our lives easier and declare them as variables.

So, in order to use just $name and $content, we have to first declare them. So, you would end up being able to do this :

Code: Select all

$name = $_POST['name'];
$content = $_POST['content'];

// you can now echo them out if you want.. or go ahead and query the database..

echo 'Name : '.$name.' <br />';
echo 'Content : .'$content.' <br />';

// other stuff ( connections, etc ).  then..
$sql = "INSERT INTO news (id, name, content) VALUES ('NULL', '".$name."', '".$content."'";
$result = mysql_query($sql);

// etc...

as you can see, we first declared the variables ( $name and $content ) based on the POST data we called. Then, we just called the variables in whatever way we needed... you cannot just call $name without first defining it ( ie, defining it to have a value of whatever $_POST['name'] was equal to ).

a good tutorial jason made up on this subject is available here if you have anymore questions relating to this topic :

viewtopic.php?t=511
[...that was the url i posted in my above post. (a hint to FOsk8r) --JAM]

hope this helps.
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

sorry info, I read the article but didnt understand due to my inadiquate php skill, and probably me inadiquate comprehension skills. No that it has a been some time and my skill levels have grown, I know understand waht you have told me, and I am going to try this out in a day or so.
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

SOLVED Connecting to MySQL

Post by FOsk8r »

I just decieded to make this all eaisery and just make the code simpler.
So, here it is

POST.HTML

Code: Select all

&lt;form name="addnews" method="post" action="post.php"&gt;
  		&lt;p class="bodymd"&gt;Name&lt;br&gt;
		&lt;input type="text" name="name"&gt;
		&lt;/p&gt;
		&lt;p class="bodymd"&gt;Content&lt;br&gt;
		&lt;textarea name="content" rows="10" cols="50"&gt;&lt;/textarea&gt;
		&lt;/p&gt;
		&lt;p class="bodymd"&gt;
		&lt;input type="submit" name="Submit" value="Submit"&gt;
		&lt;input type="reset" name="Reset" value="Clear Form"&gt;
		&lt;/p&gt;	  
	  &lt;/form&gt;
POST.PHP

Code: Select all

<?php
if (($_POST['name'] == "") || ($_POST['content'] == "")) 
{ 
   echo "Invalid Name or Content";
   echo "<br>";
   echo "<a href='post.html'>Back</a>";
exit; 
}


$conn = mysql_connect("localhost","tp4r03","") or die ("Couldn't connect to DB."); 
$db = mysql_select_db("tp4r03_uk_db") or die(MySQL_Error());  // change DB to your database name.. 
$sql = "INSERT INTO news (id, name, content)". "VALUES ('NULL', '".$_POST['name']."', '".$_POST['content']."')"; 
$result=MySQL_Query($sql) or die(MySQL_Error());



{
echo "<b>Your Content Was Added</b>";
echo "<br>";
echo "Name:  ";
echo $_POST['name'];
echo "<br>";
echo "Content:  ";
echo $_POST['content'];
}
?>
thanks again INFOLOCK.
I know understand somewhat about the $_POST['var'] function.
Heres my next problem
viewtopic.php?t=16084
Post Reply