Click here to Skip to main content
Sign Up to vote bad
good
See more: PHPMySQL
I am using this small line to select user id from my database , but I can't select it . I don't know why , because the browser doesn't display any error , and I cannot figure where the problem is. Please help me; Excuse me for the language used In this code , because it is Albanian language (the language I use in my country)
 
<html>
<head>
<title>Krijo nje artikull per kete faqe</title>
<link rel="stylesheet" href="assets/login_form/reset.css">
<link rel="stylesheet" href="assets/login_form/animate.css">
<link rel="stylesheet" href="assets/login_form/styles.css">
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
        // General options
        mode : "textareas",
        theme : "advanced",
        plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
 
        // Theme options
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,
 
        // Skin options
        skin : "o2k7",
        skin_variant : "silver",
 
        // Example content CSS (should be your site CSS)
        content_css : "css/example.css",
 
        // Drop lists for link/image/media/template dialogs
        template_external_list_url : "js/template_list.js",
        external_link_list_url : "js/link_list.js",
        external_image_list_url : "js/image_list.js",
        media_external_list_url : "js/media_list.js",});
</script>
</head>
<body>
<?php
 
mysql_connect("localhost","login_h","1234");
mysql_select_db("articles");
	if(!isset($_COOKIE['zakamezusr'])){
			header("Location: login.php");
	}
		if(isset($_POST['submit'])){
			$author_id = mysql_query("SELECT ID FROM users WHERE username ='".stripslashes($_COOKIE['zakamezusr'])."'");
			$query = "INSERT into posts (title , post ,date_up , author_id , description) VALUES ('".$_POST['title']."', '".$_POST['post']."', '".$_POST['date_up']."', '$author_id', '".$_POST['description']."')";
			mysql_query($query);
?>
<h1>sfafdadfasdfasd</h1>
<?php
}else{
?>
<div id="container" style="removed20%;removed:relative;width:640px;">
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	
 <h1>Krijo nje Artikull te ri</h1>
 <table border="0">
 
 <tr><td>Emri i Artikullit</td><td>
 
 <input type="text" name="title" maxlength="60">
 
 </td></tr>
 
 <tr><td>Pershkrimi i Artikuallit</td><td>
 
 <input type="text" name="description" maxlength="60">
 
 </td></tr>
 
 
 <tr><td>Teksti</td><td>
 
 <textarea class="mult" name="post" ></textarea>
 </td></tr>
 <tr><th colspan=2><input type="submit" name="submit" 
value="Register">
</th></tr> </table>
</form>
 
</div>
 
<?php
}
?>
 
</body>
</html>
Posted 19 Sep '12 - 8:40


1 solution

You aren't using the result from mysql_query correctly. The mysql_query function returns resource on a successful query and false otherwise. The resource is not directly the result you are after, but rather a resource which contains the result.
 
Long story short to get the result you're after you'll need to do the following after the first query:
 
$row = mysql_fetch_assoc($author_id);
$query = "INSERT into posts (title , post ,date_up , author_id , description) VALUES ('".$_POST['title']."', '".$_POST['post']."', '".$_POST['date_up']."', '$author_id".$row['ID']."', '".$_POST['description']."')";
 
If you look at http://php.net/manual/en/function.mysql-query.php[^], specifically example 2 it gives you code that allows you to retrieve the data from the result resource.
 
You should also note that use of mysql is discouraged and its recommended to use either mysqli or pdo_mysql. For a discussion on which mysql api you can look at http://php.net/manual/en/mysqlinfo.api.choosing.php[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 508
1 Arun Vasu 275
2 Mahesh Bailwal 259
3 Maciej Los 238
4 Rohan Leuva 176
0 Sergey Alexandrovich Kryukov 9,660
1 OriginalGriff 7,329
2 CPallini 3,968
3 Rohan Leuva 3,339
4 Maciej Los 2,851


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid