Jump to content


How can I create MySql form?


10 replies to this topic

#1 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 22 August 2007 - 12:25 AM

I want to make a form that will input data into my database like what Dadabik does but with my own personal touches and form's. I have a reviews site and I other people to input database as well.

I followed this tutorial but somehow it doesn't work for me.

I type in the database and press submit and it goes through and echo's Success! but when I go to phpmyadmin through my cPanel to check if it worked, it didn't show up! Here is the code:
<?php
mysql_connect ("xxx", "xxx", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db (xxx);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Title: <input type="text" name="name"><br>
Summary: <input type="text" name="summary"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST['title'];
$summary = $_POST['summary_synopsis'];
mysql_query("INSERT INTO `anime_reviews` (title, summary_synopsis) VALUES ('$name', '$summary')");
echo "Success! Your RomanceAnime.com Review has been added! Now go eat your own poop! :huh:";
}
?>
</body>
</html>

Perhaps a link to a better tutorial or a guide to make a 'front-end'

Edited by Dat, 22 August 2007 - 01:03 AM.


#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 22 August 2007 - 03:46 AM

You are trying to insert the wrong values into the database. The values you are using in the $_POST are not set to what the names of the inputs are:

} else {

	if( mysql_query( "INSERT INTO `anime_reviews` SET `title` = '".mysql_real_escape_string( $_POST['name'] )."', `summary_synopsis` = '".mysql_real_escape_string( $_POST['summary'] )."' ") ) {
		echo "Success! Your RomanceAnime.com Review has been added! Now go eat your own poop! tongue.gif";
	} else {
		echo mysql_error();
	}

}

Should fix your problem.

Matt

#3 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 22 August 2007 - 01:56 PM

Thanks for the help but now when I input the data it says "No database selected" Now

<?php
mysql_connect ("localhost", "xxUserxx", "xxPassxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db (anime_reviews);
?>
<html>
<head>
<title>Anime review</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Title: <input type="text" name="name"><br>
Summary: 
<textarea name="summary"></textarea>
<br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {

	if( mysql_query( "INSERT INTO `anime_reviews` SET `title` = '".mysql_real_escape_string( $_POST['name'] )."', `summary_synopsis` = '".mysql_real_escape_string( $_POST['summary'] )."' ") ) {
		echo "Success! Your RomanceAnime.com Review has been added! Now go eat your own poop!";
	} else {
		echo mysql_error();
	}

}
?>
</body>
</html>

Edited by Dat, 22 August 2007 - 01:57 PM.


#4 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 22 August 2007 - 02:14 PM

Hmmm... I could be wrong:

mysql_select_db (anime_reviews);

Should have quotes in it.

#5 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 22 August 2007 - 02:31 PM

It changed the color display in my editor but it didn't fix the problem.
I don't think it made a difference "" or not. Thanks for trying.

#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 22 August 2007 - 05:05 PM

View PostDat, on Aug 22 2007, 02:31 PM, said:

It changed the color display in my editor but it didn't fix the problem.
I don't think it made a difference "" or not. Thanks for trying.

Yes it does. You are entering a string, not a constant.

Try this for that line for some error reporting.

mysql_select_db('anime_reviews') or die('I cannot select the database because '.mysql_error());


#7 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 22 August 2007 - 05:17 PM

View PostDemonslay, on Aug 22 2007, 03:05 PM, said:

View PostDat, on Aug 22 2007, 02:31 PM, said:

It changed the color display in my editor but it didn't fix the problem.
I don't think it made a difference "" or not. Thanks for trying.

Yes it does. You are entering a string, not a constant.

Try this for that line for some error reporting.

mysql_select_db('radata') or die('I cannot select the database because '.mysql_error());

Is there a difference in outputting the error between this

mysql_connect ("xxx", "xxx", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());

And what you gave me?

EDIT: Database name should be radata //I fixed the error.
EDIT: There is a difference after all, I got this error report: I cannot select the database because Access denied for user 'xxuserx'@'localhost' to database 'radata'

EDIT: Fixed it works now - typo on the database name I was missing the prefix, all thanks to the error report

Edited by Dat, 22 August 2007 - 05:22 PM.


#8 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 22 August 2007 - 05:22 PM

See. :)

Simple access problem. Go through your chosen method of dealing with MySQL and alter the user permissions for the user.

I'm sure you're already doing that at the moment though, lol.

Edit
Good to hear you got it working just as I was posting, lol.
Just remember to always use error reporting with MySQL. This is the absolute biggest (and most annoying to helpers like me) thing that alot of beginners run into, when they keep asking what's with the whole 'invalid parameter, expecting resource' is thrown by PHP's MySQL dealing functions.
Always use 'or die(mysql_error())' after functions such as mysql_query() and the connection functions.

Edited by Demonslay, 22 August 2007 - 05:25 PM.


#9 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 23 August 2007 - 02:17 PM

I have another situation that I would like to do with my form so I guess I will post my problem here.

I want to create a form where I insert my reviews data into my database

Alright here is the problem, I don't know how I can input data into my database using Checkboxes in my form! I looked at this tutorial but in this tutorial it is set up for a new table. I don't want that, I already have reviews on my site and I don't want to create new table's on my database.

So the question is: How can I input data using checkboxes without altering my database tables and such?

There are more problems like: Length, and Genre (The checkboxes that I am referring to) Look at the code and you will know what I mean. Any help would be much appreciated!

<?php
//Connect database
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>


<form action="" method="post">
Title: <input type="text" name="title"><br>
Japanese Title: <input type="text" name="japtitle"><br>
Summary: <textarea name="summary" cols="50" rows="5"></textarea>
<br>
Genres: <input type="text" name="genres"><br>
Formats: <label><input name="formats" type="checkbox" value="romance" checked>
Romance
</label>
<label><input name="formats" type="checkbox" value="comedy">
Comedy
</label>
<label><input name="formats" type="checkbox" value="horror">
Horror
</label>
<label><input name="formats" type="checkbox" value="ecchi">
Ecchi
</label>
<br>
Number of Episodes: <input name="numberepisodes" type="text" size="3">
<br>
Length:
<label><select name="length" type="select">
  <option value=""> -</option>
  <option value="30 Minutes / Half hour">30</option>
  <option value="1 Minutes">1</option>
</select>
</label>
  or  
<label>
<input name="length" type="text" size="3">
</label>
<br>
Year Published: <input type="text" name="year"><br>
Opening Theme: 
<textarea name="openingtheme"></textarea>
<br>
Ending Theme: 
<textarea name="endingtheme"></textarea>
<br>
Official Website: <input type="text" name="website"><br>
<input type="submit" name="submit" value="Submit!">
</form>
</body>
</html>


<?php
} else {
	if( mysql_query( "INSERT INTO `anime_reviews` SET `title` = '".mysql_real_escape_string( $_POST['title'] )."', `title_jap` = '".mysql_real_escape_string( $_POST['japtitle'] )."', `summary_synopsis` = '".mysql_real_escape_string( $_POST['summary'] )."', `genres` = '".mysql_real_escape_string( $_POST['genres'] )."', `episode_number` = '".mysql_real_escape_string( $_POST['numberepisodes'] )."', `length` = '".mysql_real_escape_string( $_POST['length'] )."', `year_published` = '".mysql_real_escape_string( $_POST['year'] )."', `opening_theme` = '".mysql_real_escape_string( $_POST['openingtheme'] )."', `ending_theme` = '".mysql_real_escape_string( $_POST['endingtheme'] )."', `official_sites` = '".mysql_real_escape_string( $_POST['website'] )."' ") ) {
		echo "Success! Your RomanceAnime.com Review has been added! Congrats";
	} else {
		echo mysql_error();
	}
}
?>


#10 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 23 August 2007 - 03:38 PM

Well, you can't just store data in a database without actually putting it some place.

If you are needing to add more data, you'll simply have to alter the database table and add more columns.

#11 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 23 August 2007 - 04:23 PM

View PostDemonslay, on Aug 23 2007, 01:38 PM, said:

Well, you can't just store data in a database without actually putting it some place.

If you are needing to add more data, you'll simply have to alter the database table and add more columns.
No I have a place to store it, but I don't know how!

Click here! for example's of my pages.

Edited by Dat, 23 August 2007 - 04:24 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users