PHP MySQL Help, contents not inserting.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 895 Views - Last Post: 18 December 2008 - 08:11 PM Rate Topic: -----

#1 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 07:47 AM

I'm trying to set up a collaborative archive of content from a site, and don't want to allow people to upload anything they want, so I decided to write a form that should write the content of the form to a database, but it doesn't write it.

My code for the upload script is:
<?php
switch (@$_POST['html'])
{
case "upload":

include("dbstuff.inc");
$cxn = mysqli_connect($host,$user,$passwd,$dbname);

$title = htmlspecialchars($_POST['title']);
$article = htmlspecialchars($_POST['article']);
$author = htmlspecialchars($_COOKIE['name']);
$comment = htmlspecialchars($_POST['comment']);

$add_art = addslashes($article);
$add_title = addslashes($title);

$final = nl2br($add_art);


$date = date('Y-m-d');
$time = date('h:i:s');
$cxn;
$insert = "INSERT INTO 'uploaded' ('title', 'body', 'insert_time', 'insert_date', 'author', 'comment') VALUES ('$add_title', '$final', '$time', '$date', '$author', '$comment')";
mysqli_query($cxn,$insert);


if($insert){
	echo "Ok.";
}else{
	echo "Nope.";
}

mysqli_close($cxn);

default:include("upload.inc");
}?>



and for the form:
<?include("header.php")?>
<title>Submit a file</title>
</head>
<body>
<?include("nav.php")?>
<div id="content">

<form action=<?php echo $_SERVER['PHP_SELF']?> method="POST">
<table style="font-family:arial;" cellpadding="8">
		   <tr valign="top">
		 	 <td>Title:</td>
			 <td><input type="text" name="title" /></td>
		 </tr>
		 <tr valign="top">
		 	 <td>Article:</td>
			 <td><textarea cols="40" rows="20" name="article"></textarea></td>
		 </tr>
		 <tr>
		 	 <td>Additional Comments</td>
			 <td><textarea cols="40" rows="10" name="comment"></textarea></td>
		 </tr>

</table>
<table>
		 <tr>
		 	 <td><input type="submit" name="html" value="upload"/></td>
		 </tr>
</table>
</form>
</div>
<?include("footer.php")?>



Is This A Good Question/Topic? 0
  • +

Replies To: PHP MySQL Help, contents not inserting.

#2 xerxes333  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 504
  • Joined: 05-July 07

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 08:13 AM

What do you mean by . . . but it doesn't write it. ? Are you getting an error, is the data being truncated, what specifically is not working they way you expect?
Was This Post Helpful? 0
  • +
  • -

#3 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 08:15 AM

I mean the data isn't showing up in the database.
Was This Post Helpful? 0
  • +
  • -

#4 LoganLooker  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 01-February 07

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 08:31 AM

But, their should be some kind of error if the data isn't showing up in the database unless your looking in the wrong database?
Was This Post Helpful? 0
  • +
  • -

#5 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 08:36 AM

There should be one, but there isn't.

It's quite frustrating.
Was This Post Helpful? 0
  • +
  • -

#6 samf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 93
  • Joined: 11-August 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 09:05 AM

Echo your insert statement and copy paste what is displayed here.
Was This Post Helpful? 0
  • +
  • -

#7 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 09:09 AM

INSERT INTO 'uploaded' ('title', 'body', 'insert_time', 'insert_date', 'author', 'comment') VALUES ('lolol', 'aksfshfqlk', '07:58:08', '2008-12-17', 'KoolAidRelic', 'gqwe')

Just as a quick test.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 09:17 AM

Try it this way:
$insert = "INSERT INTO uploaded (title, body, insert_time, insert_date, author, comment) VALUES ('$add_title', '$final', '$time', '$date', '$author', '$comment')";
Was This Post Helpful? 1
  • +
  • -

#9 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 09:29 AM

Thank you so much! It works now!
Was This Post Helpful? 0
  • +
  • -

#10 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 10:52 AM

OK, now I feel so stupid.

How would I display a list of the files, something like:
<tr class="row1"><td><a href="?primaryid">insert_date</a> | title - author</td></tr>
<tr class="row2"><td><a href="?primaryid">insert_date</a> | title - author</td></tr>
<tr class="row1"><td><a href="?primaryid">insert_date</a> | title - author</td></tr>

? Or is that possible?

And on clicking the link it displays the content of the article that has that primary ID.

This post has been edited by relic5.2: 17 December 2008 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 11:30 AM

I would echo the HTML with the appropriate variables. I can't do it now because I'm on my iPhone. Will check in later.
Was This Post Helpful? 0
  • +
  • -

#12 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 11:36 AM

Thanks!
Was This Post Helpful? 0
  • +
  • -

#13 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 04:25 PM

Could try something like this:

$query = sprintf("SELECT * FROM uploaded);
$result = mysql_query($query) or die(mysql_error());
$counter = 1;
while($row = mysql_fetch_assoc($result)) {
  if($counter) {
	echo "<tr class='row1'>;
	$counter = 0;
  }
  else {
	echo "<tr class='row2'>";
	$counter = 1;
  }
  echo "<td><a href="{$row['primaryid']}">{$row['insert_date']}</a> | {$row['title']} - {$row['author']}</td></tr>\r\n";
}



This will take everything out of the 'uploaded' table, and list them all using your alternating row classes, using the data from the database. If any of those values are incorrect for what you're wanting to put in there, simply change the key on the array call that's incorrect.

mysql_fetch_assoc() takes the results of a MySQL query and puts the next row into an array with the variable name you specify. In this case, $row. Subsequent calls to this function (which we do as the condition for the while() loop) advance the row pointer by one, and when it runs out of rows, it returns false, thereby exiting our while() loop.

Hope this helps!

PS: The "\r\n" just makes the 'View Source' on your code look cleaner :)

This post has been edited by Valek: 17 December 2008 - 04:26 PM

Was This Post Helpful? 1
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: PHP MySQL Help, contents not inserting.

Posted 17 December 2008 - 06:23 PM

Valek's solution looks good to me, and I'm tired. Post again if you need more and I'll try to live up to my word!
Was This Post Helpful? 0
  • +
  • -

#15 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP MySQL Help, contents not inserting.

Posted 18 December 2008 - 06:28 PM

Thank you so much! It works, and better yet, it works fast!

Now I have one more quick question, can I call the contents of the "article" on some page where the url is list.php?page={primaryid} ?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2