Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
<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

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:

PHP
$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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900