justlukeyou Posted March 26, 2011 Share Posted March 26, 2011 I have peice of code which is designed enter a question into a database and the username of the person who asks the question. However, the code enters <?php echo Array; ?> into the database and not 'Tom'. I am using the same code which inserts the category of the question in the database which works. But the username comes up as <?php echo Array; ?>. Does anyone know why it shows "array"? if($loggedIn) { echo "Welcome, ".$user['username'].". <a href=\"logout.php\">Logout</a>. <table width='300' border='0' align='center' cellpadding='0' cellspacing='1'> <tr> <td><form name='form1' method='post' action='phpviewquestion.php'> <table width='100%' border='0' cellspacing='1' cellpadding='3'> <tr> <td colspan='3'><strong>Your Question</strong></td> </tr> <tr> <td width='71'>Question</td> <td width='6'>:</td> <td width='600' height='50'><input name='question' type='text' id='question'></td> <td width='71'>Notes</td> <td width='6'>:</td> <td width='600' height='50'><input name='notes' type='text' id='notes'></td> </tr> <tr> <td colspan='3' align='center'><input type='image' name='image' value='Submit' src='http://www.domain.co.uk/images/submitbutton.PNG' name='image' width='100' height='53'></td> <input name='category' type='hidden' value='Furniture' id='category' > <input name='questionmaker' type='hidden' value='<?php echo $username; ?>' id='questionmaker' > </tr> </table> </form> </td> </tr> </table> </div> " ; } else { echo "Please <a href=\"login.php\">Login</a>."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/ Share on other sites More sharing options...
trq Posted March 26, 2011 Share Posted March 26, 2011 You are already within a php block, so the <?php ?> are taken literally as strings. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192344 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Thanks, I changed it to this and now it shows it as blank, I can get it to work with category but when I try to insert a string it shows at blank. <input name='questionmaker' type='hidden' value='$user['username']' id='questionmaker' > Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192348 Share on other sites More sharing options...
creata.physics Posted March 26, 2011 Share Posted March 26, 2011 It look's like it would input Array['username'] to me. Did you try? <input name='questionmaker' type='hidden' value='".$user['username']."' id='questionmaker' > or <input name='questionmaker' type='hidden' value='{$user['username']}' id='questionmaker' > looks like it'd work, if your previous code from the first post hasn't changed, and just that line has. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192353 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Hi, It now shows as "Array". How can it turn a username which is echod onto the form page into "Array" onto the page which inserts the entry into the database? Very frustrating. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192355 Share on other sites More sharing options...
nethnet Posted March 26, 2011 Share Posted March 26, 2011 At the top of the script where you have this... echo "Welcome, ".$user['username'] etc.. does that properly show 'Tom' on the webpage, or does it also show 'Array'? If it shows 'Tom' then it's a simple matter of a typo in your script. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192359 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Hi, It does shows as 'Tom' but I cant see the typo. If enter "123" then this enters into the database as "123" but it I try to use a string then just shows as "array". if($loggedIn) { echo "Welcome, ".$user['username'].". <a href=\"logout.php\">Logout</a>. <table width='300' border='0' align='center' cellpadding='0' cellspacing='1'> <tr> <td><form name='form1' method='post' action='phpviewquestion.php'> <table width='100%' border='0' cellspacing='1' cellpadding='3'> <tr> <td colspan='3'><strong>Your Question</strong></td> </tr> <tr> <td width='71'>Question</td> <td width='6'>:</td> <td width='600' height='50'><input name='question' type='text' id='question'></td> <td width='71'>Notes</td> <td width='6'>:</td> <td width='600' height='50'><input name='notes' type='text' id='notes'></td> </tr> <tr> <td colspan='3' align='center'><input type='image' name='image' value='Submit' src='http://www.ukhomefurniture.co.uk/images/submitbutton.PNG' name='image' width='100' height='53'></td> <input name='category' type='hidden' value='Furniture' id='category' > <input name='questionmaker' type='hidden' value='{$user['username']}' id='questionmaker' > Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192361 Share on other sites More sharing options...
nethnet Posted March 26, 2011 Share Posted March 26, 2011 You say if you enter a number, it will show properly. But where are you entering a value? Where is your $user array declared? If we see that, we may be able to better pinpoint the issue. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192362 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Do I need to declare 'user' on the form? This is the form if($loggedIn) { echo "Welcome, ".$user['username'].". <a href=\"logout.php\">Logout</a>. <table width='300' border='0' align='center' cellpadding='0' cellspacing='1'> <tr> <td><form name='form1' method='post' action='phpviewquestion.php'> <table width='100%' border='0' cellspacing='1' cellpadding='3'> <tr> <td colspan='3'><strong>Your Question</strong></td> </tr> <tr> <td width='71'>Question</td> <td width='6'>:</td> <td width='600' height='50'><input name='question' type='text' id='question'></td> <td width='71'>Notes</td> <td width='6'>:</td> <td width='600' height='50'><input name='notes' type='text' id='notes'></td> </tr> <tr> <td colspan='3' align='center'><input type='image' name='image' value='Submit' src='http://www.ukhomefurniture.co.uk/images/submitbutton.PNG' name='image' width='100' height='53'></td> <input name='category' type='hidden' value='Furniture' id='category' > <input name='questionmaker' type='hidden' value='{$user['username']}' id='questionmaker' > it is declared here as questionmaker // Get values from form $category = $_POST['category']; $question=$_POST['question']; $notes=$_POST['notes']; $questionmaker=$_POST['questionmaker']; $qid = $row['qid']; // Insert data into mysql $sql="INSERT INTO $tbl_name(category, question, notes, questionmaker)VALUES('$category', '$question', '$notes', '$questionmaker')"; $result=mysql_query($sql); $qid = mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192363 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Hi, Can anyone help me with this please. I just cant see what the problem is. I cant see any typo's. If I enter 123 manually this enters into the database but not the string. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192482 Share on other sites More sharing options...
monkeytooth Posted March 26, 2011 Share Posted March 26, 2011 Ok lemme see if I got this right, you want in the form element to have it populated automaticly through some echo variable output? but when you submit it doesnt have anything there you get an error, or whatever correct? But if you manually type into the form element the value it works... The value you are trying to output, only outputs "Array" am I correct so far? The vaule thats putting out "Array" is actually echoing out what it is, which is an Array.. in that first line you showed in your first post <?php echo Array; ?> whats the actual $value there? I know it can't be "Array" in a literal sense of the word. So do this, since its an array item, put: echo "<pre>"; print_r($value); echo "</pre>"; replacing the: $vaule with whatever your $VarName is thats echoing out the word "Array" then copy and paste that here. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192487 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Hi, It prints the username of database name (dbo123456789 for example) which is very concerning. So I should I change the column name will this suffice. I can work out why it printing the tablename. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192504 Share on other sites More sharing options...
monkeytooth Posted March 26, 2011 Share Posted March 26, 2011 thats likely due to either incorrect variable choice in the print_r statement, or a clash in variable names somewhere through the rest of that particular files variables, or through multiple files that are included in to that file and there variables. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192508 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Ive changed the username as I was using that twice. I have changed the name of the member to 'membername'. Now I just trying to echo this. Quote Link to comment https://forums.phpfreaks.com/topic/231741-shows-in-database/#findComment-1192509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.