/    Sign up×
Community /Pin to ProfileBookmark

php code not inserting into phpmyadmin

HI everyone…I had this working at one time but must have changed something. I cant get the php to insert records into phpmyadmin. Can someone check if I have something wrong in my code. Thanks much.

[COLOR=”Red”]Here is the code for selecting from the database:[/COLOR]

[code=php]<?php
mysql_connect(“localhost”, “root”, “”);
mysql_select_db(“mydb”);
?>[/code]

<html>
<head>
<title>Project 3 Blog</title>
</head>
<body>

[code=php]<?php
$sql = “SELECT * FROM posts”;
$query = mysql_query($sql);
$result = mysql_fetch_array($query);

$title = $row[‘title’];
$description = $row[‘description’];

?>[/code]

<table border=’1′>
<tr><td><?php echo $title; ?></td></tr>
<tr><td><?php echo $description; ?></td></tr>
</table>

[code=php]<?php
}
?>[/code]

</body>
</html>

[COLOR=”Red”]Here is the code for inserting into the database:[/COLOR]

[code=php]<?php
mysql_connect(“localhost”, “root”, “”);
mysql_select_db(“mydb”);
?>
[/code]

<html>
<head>
<title>Add new Post</title>
</head>
<body>

[code=php]<?php
if(isset($_POST[‘submit’])){
$title = $_POST[‘title’];
$description = $_POST[‘description’];

mysql_query(“INSERT INTO posts (title, description) VALUES(‘$title’,’$description’)”);

}else{

?>[/code]

<form action=’admin.php’ method=’post’>
Title: <input type=’text’ name=’title’ /><br>
Description: <textarea name=’description’></textarea><br />
<input type=’submit’ name=’submit’ value=’Post’ />
</form>

[code=php]<?php
}
?>[/code]

</body>
</html>

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@googenfrogauthorMay 06.2012 — Nothing is wrong with this. I just realized I can't put special characters in the title and description. How do I keep the users from putting those in the text. I didn't realize that kept the text from being entered in mysql. Is this something that has to be formatted somewhere in the code. Thanks again
Copy linkTweet thisAlerts:
@NogDogMay 06.2012 — Just be sure to escape all user inputs before using them in a query. Besides avoiding problems like this, it also stops malicious users from injecting SQL into your queries. With the MySQL extension, the function you want to use is mysql_real_escape_string().
Copy linkTweet thisAlerts:
@ScottyBoyMay 07.2012 — I also run most data through [url=http://php.net/manual/en/function.htmlspecialchars.php]htmlspecialchars()[/url]. It converts HTML to character codes, so it doesn't effect the page.
Copy linkTweet thisAlerts:
@NogDogMay 08.2012 — I also run most data through [url=http://php.net/manual/en/function.htmlspecialchars.php]htmlspecialchars()[/url]. It converts HTML to character codes, so it doesn't effect the page.[/QUOTE]

Note that the htmlspecialchars() (or htmlentities()) is normally reserved for escaping text being output to the browser; I do not recommend it for escaping inputs into the database. Why? Because then in your database you might end up with something like this:
<i>
</i>Some text &amp;amp; &amp;quot;some quoted text&amp;quot;

This can have two unwanted side effects: (1) It could mean text that would otherwise just fit within a char/varchar column might now be too long, and (2) it makes searches of the data more problematic. But it is definitely a good idea for text being output to the browser, e.g.:
[code=php]
<input type='text' name='foo' value='<?php echo htmlspecialchars($row['text']);?>' />
[/code]
Copy linkTweet thisAlerts:
@ScottyBoyMay 09.2012 — Note that the htmlspecialchars() (or htmlentities()) is normally reserved for escaping text being output to the browser; I do not recommend it for escaping inputs into the database. Why? Because then in your database you might end up with something like this:
<i>
</i>Some text &amp;amp; &amp;quot;some quoted text&amp;quot;

This can have two unwanted side effects: (1) It could mean text that would otherwise just fit within a char/varchar column might now be too long, and (2) it makes searches of the data more problematic. But it is definitely a good idea for text being output to the browser, e.g.:
[code=php]
<input type='text' name='foo' value='<?php echo htmlspecialchars($row['text']);?>' />
[/code]
[/QUOTE]


Good thinking. I'll definitely keep that in mind for future use. ?
×

Success!

Help @googenfrog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...