php submitting the username

Joined
12 Feb 2006
Posts
17,643
Location
Surrey
i have a form submitting itself to mysql and then php reading it, which for me, is just brilliant.

something im stuck with now is how would i get the form/php to submit which user submitted the content and then display that content, rather then atm i have it so they have to put a name.

the form is set up so it only will show if the user is signed in, but it thens how would i get the form to say, find username and submit it as something like author?
 
You say you already have a user login, how are you achieving this? If you're already using session variables then it's simply a case of storing the username in the session and then inserting it along with the form data.
 
Yeah, when the login is authenticated, you just need to do:

Code:
$_SESSION['username'] = $username;

Then when you add the content to the DB, just do:

Code:
$username = $_SESSION['username'];

mysql_query("INSERT INTO comments(title,content,user) VALUES('$title','$content','$username')");

Make sure you're checking your input first though with mysql_real_escape_string() and strip_tags() :)
 
perfect, just what i needed to know.

while im here. when sites are put on a server i know that the file name needs to be index.html for the homepage, what do you do if you want the home page for that domain to be index.php?

thanks

[edit]
also anther thing that would be great to know, how would i g about making a way for the user to upload a picture to go with what they submit? im not looking step by step obviously, just advice where to start, e.g. am i in need first of making a way to upload things, then i want to sort out the rest?
thanks
 
Last edited:
addy_010 said:
this is what i want. Im on the file located at root/submit/adminpanel.php and i want it to link back to root/add/addnews.php, or even root/index.php. How would this be done?
/index.html in root/folder/file/wherever/here.html goes to root/index.html

../index.html in root/folder/more/here.html goes to root/folder/index.html

addy_010 said:
while im here. when sites are put on a server i know that the file name needs to be index.html for the homepage, what do you do if you want the home page for that domain to be index.php?
just delete index.html, it should work for index.php automatically

addy_010 said:
also anther thing that would be great to know, how would i g about making a way for the user to upload a picture to go with what they submit? im not looking step by step obviously, just advice where to start, e.g. am i in need first of making a way to upload things, then i want to sort out the rest?
create an input of the type "File" in the submitting form, then when processing the file in php it's something like:

PHP:
//check that there's a file
if(is_uploaded_file($_FILES['filefieldname']['tmp_name'])){

    //get filename extension
    $ext=strrchr($_FILES['filefieldname']['name'], ".");

    //create a filename
    $file_place=$_SERVER['DOCUMENT_ROOT'].'/images/myimage'.$ext;

    //move from temporary upload location to permanent location
    if(move_uploaded_file($_FILES['filefieldname']['tmp_name'],$file_place)){
         echo "file moved to $file_place";
    }else{
         echo "didnt move file";
    }
    
}

(i bet i've missed something out there though, 'tis typical of me at 1:30 am :p)
 
ok i have managed to implement a php script for uploading images, which did work, for some odd reason isn't now but i know i can sort that in a bit. the problem im having now is that i want the image that is uploading to be associated with the rest of the information in the form, so that when icall whats on the form for id 1 etc, it brings the image that was uploaded at the same time, if there is one, if not it displays a default image.

here is the php on the page submit.php
Code:
<? include("../login/include/session.php"); ?>

<?php

include"connect.php"; 

if(!isset($_POST['add_news']))
	{
echo"
<br />
<br />
<form action='$self?action=addnews' name='addnews' method='post' enctype=\"multipart/form-data\">
<table>

	<tr>
		<td>
			Title
		</td>

		<td>
			<input type='text' name='title'>
		</td>
	</tr>

	<tr>
		<td>
			Description:
		</td>

		<td>
			<textarea cols='15' name='description' rows='5'></textarea>
		</td>
	</tr>

	<tr>
		<td>
			URL: http://
		</td>

		<td>
			<input type='text' name='url'></input>
		</td>
	</tr>
	
	<tr>
		<td>
			Upload Image:
		</td>	
	
		<td>
			<input type='file' name='imgfile'></input>
		</td>
	</tr>
	
	<tr>
		<td>
			<input type='submit' name='add_news' value='submit news topic'>
		</td>
	</tr>
	
</table>
</form>";	
}
elseif(isset($_POST['add_news'])) 
{
$title = mysql_real_escape_string($_POST['title']);
$url = mysql_real_escape_string($_POST['url']);
$description = $_POST['description'];
$date = date("d.m.y");
$username = $_SESSION['username'];

$error = array();

if(empty($title)) {
$error[] = "You must enter a title!";
}
if(empty($description)) {
$error[] = "Enter a description please"; 
}
if(empty($url)) {
$error[] = "The URL is needed"; 
}

if(count($error)>0) {
echo"<font size='3' color='#CC0000'><strong>ERROR:</strong></font>";
foreach($error as $error2);
echo"$error2";
}
else {
$sql = "INSERT INTO submit(title, description, url, author, date, valid) VALUES('$title', '$description', '$url', '$username', '$date', '0')";

mysql_query($sql);
echo"Website successfully queued for verification. Your site will be added when verified by an Admin. Check back soon to see it. An email will be sent if there is any problems.";
}
	}
	
  if(isset($_POST['submit'])) {

$uploadpath = "upload/";

$uploadpath = $uploadpath . basename( $_FILES['imgfile']['name']); 

if(move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadpath)) {


	echo"This file: ".  basename( $_FILES['imgfile']['name']). "has been uploaded Other Details:";
	echo"Mime Type: ". $_FILES['imgfile']['type'] ."";
	echo"Size (Bytes): ". $_FILES['imgfile']['size'] ."";
	echo"File Name: ". $_FILES['imgfile']['name'] ."";

} 
else {
	echo "There was an error uploading the file, please try again!";
}
  }


?>

i know that i need to add the name for the INSERT INTO part but i have no clue how i'd get the name if that makes sense.

hoping someone can help me out here
 
Back
Top Bottom