Angus Pearson Posted July 4, 2011 Share Posted July 4, 2011 Hi, I couldn't find a topic that was relevant, so here goes: Pretymuch the first major PHP script i am atempting to write has kinda died: It's only a few lines long, and the small bit of MySQL connection code doesn't work for some reason that I can't work out. This is the code: index.php: <?php include('includes/functions.php'); getPosts(); ?> connect.php: <?php define('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PASS',''); define('DB_NAME','cmsdb'); $connection = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME) or die(mysql_error()); ?> functions.php: <?php include('includes/connect.php'); function getPosts() { $query = mysql_query("SELECT * FROM posts") or die(mysql_error()); while($post = msql_fetch_assoc($query)) { echo "<h2>" . $post['Title'] . " by" . $post['Author'] . "</h2>"; echo $post['Content']; } } ?> errors: Fatal error: Call to undefined function msql_fetch_assoc() in C:\xampp\htdocs\cms\includes\functions.php on line 7 Link to comment Share on other sites More sharing options...
Antonio Conte Posted July 4, 2011 Share Posted July 4, 2011 Try to read the error messages. It says a function is undefined. That suggest you to check spelling, if you use the function right, etc. The reason why you got this error is msql_fetch_assoc(). I should be mysql_fetch_assoc(). Link to comment Share on other sites More sharing options...
Angus Pearson Posted July 5, 2011 Author Share Posted July 5, 2011 Try to read the error messages. It says a function is undefined. That suggest you to check spelling, if you use the function right, etc. The reason why you got this error is msql_fetch_assoc(). I should be mysql_fetch_assoc(). thanks for replying... as i suggested, i am a noob Link to comment Share on other sites More sharing options...
Antonio Conte Posted July 6, 2011 Share Posted July 6, 2011 Everyone starts out a noob. I could't understand why the error messages were even important in the beginning. They made no sense to me... After reading this book by Larry, I think you will evolve very much as a PHP programmer. It's not the most technical. It does not give you wings to fly with. But it's a very good book that makes you understand PHP. That is why no-one even come close to Larry as a writer for me, even though I've learned more advanced stuff elsewhere too. Larry's advanced book also teaches you to handle a lot of common problems and gives you a nice poker hand of solutions than may come in handy. Read his books one time, try some of it, read it quicker a second time, and you will see progress. It's actually a promise... Link to comment Share on other sites More sharing options...
HartleySan Posted July 6, 2011 Share Posted July 6, 2011 I agree with Antonio. Larry's PHP & MySQL book will give you a better start than you can get from any other resource out there. Link to comment Share on other sites More sharing options...
Angus Pearson Posted July 8, 2011 Author Share Posted July 8, 2011 Thanks for the help and support - However, as those errors were cleared up, everything worked, but now I am trying to put an edit feature in my simple CMS, but I am hitting a lot of walls: Firstly, I have to escape EVERY apostrophe and other special character otherwise I get a MySQL error, and now no matter what I do in the code, I get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Div_Class' = 'tpost', 'Title' = '2011-07-06', 'Date' = 'Hello World - Post 1', at line 1 Basically, this is how it's supposed to work: Function gets current data ----------> The User can Edit that data, ----------> A simple PHP file puts it from MySQL, and puts it in then submits it through a function that inserts the input boxes. the new data into MySQL But this hasn't worked. Here is the code: (I won't include HTML and the form) //From cms_functions.php function editPost($Div_Class, $Title, $Date, $Author, $Content, $id) { $id = (int) $id; $query = mysql_query("UPDATE posts SET 'Div_Class' = '$Div_Class', 'Title' = '$Title', 'Date' = '$Date', 'Author' = '$Author', 'Content' = '$Content' WHERE ID ='$id'") or die(mysql_error()); header("Location: posts.php"); } -------------------------------------------------------- //From edit.php <tr> <td class="descriptor"><label for="Div_Class">Class: (tpost / ipost / cpost)</label></td> <td><input type="text" name="Div_Class" class="inputother" value="<?php echo $post['Div_Class']; ?>" /></td> </tr> Link to comment Share on other sites More sharing options...
Angus Pearson Posted July 8, 2011 Author Share Posted July 8, 2011 Sorry for the second post... Just discovered what tab does... //From edit.php <form action="doedit.php" method="post"> <table> <tr> <td class="descriptor"><label for="Div_Class">Class: (tpost / ipost / cpost)</label></td> <td><input type="text" name="Div_Class" class="inputother" value="<?php echo $post['Div_Class']; ?>" /></td> </tr> //And so on... All of the variables have this layout, and $id is in a hidden input //From doedit.php <?php include('includes/cms_functions.php'); if(isset($_POST['submit'])) { if(isset($_POST['Div_Class'])) { if(isset($_POST['Date'])) { if(isset($_POST['Title'])) { if(isset($_POST['Author'])) { editPost($_POST['Div_Class'], $_POST['Date'], $_POST['Title'], $_POST['Author'], $_POST['Content'], $_POST['id']); header ("Location: posts.php"); } else { echo "Add the Author.."; include ("create.php"); } } else { echo "Add the Title..."; include ("create.php"); } } else { echo "Add the Date..."; include ("create.php"); } } else { echo "Add the Div Class..."; include ("create.php"); } } else { header("Location: create.php"); } When I changed the action to addPost, it worked fine, and added a new post. I am kinda stuck for what to do. function addPost($Div_Class, $Title, $Date, $Author, $Content) { $query = mysql_query("INSERT INTO posts VALUES(null, 0, '$Div_Class', '$Title', '$Date', '$Author', '$Content')") or die(mysql_error()); } Link to comment Share on other sites More sharing options...
Antonio Conte Posted July 8, 2011 Share Posted July 8, 2011 ".......'Title' = '2011-07-06', 'Date' = 'Hello World - Post 1', at line 1 Hope you can spot your error. Strings don't work inside datetime datatypes, you know Link to comment Share on other sites More sharing options...
Angus Pearson Posted July 8, 2011 Author Share Posted July 8, 2011 ".......'Title' = '2011-07-06', 'Date' = 'Hello World - Post 1', at line 1 Hope you can spot your error. Strings don't work inside datetime datatypes, you know Yea, but I don't know why it is trying to insert the variables into the wrong column. All of the inputs in the HTML have the correct names, and the only thing I could think of is whether the order of the variables in the function statement effect this??? Should these produce different results? e.g.: function getPosts($id, $Title, $Date) //and function getPosts($Date, $Title, $id) Link to comment Share on other sites More sharing options...
Paul Swanson Posted July 8, 2011 Share Posted July 8, 2011 Yes, those would produce different results. Functions use the arguments passed to them in the same order as the function definition, not by the names. The names are for the benefit of us poor humans, so we can more easily understand what the variable is supposed to represent. Say you have a function that takes 3 arguments, and prints a sentence from them: function make_sentence($word1, $word2, $word3) { echo "Hey $word1, compare $word2 to $word3 and let me know what you think."; } You can call this function and submit 3 strings for the variables: make_sentence('Fred', 'paper', 'pencil'); // Hey Fred, compare paper to pencil and let me know what you think. In the function, the first argument passed to it will be assigned to $word1 because that is the first argument in the function definition. It doesn't matter what name is given to the argument. Consider this: $word1 = 'Fred'; $word2 = 'paper'; $word3 = 'pencil'; make_sentence($word1, $word2, $word3); // same output as example 2 make_sentence($word2, $word3, $word1); // Hey paper, compare pencil to Fred and let me know what you think. I hope that helps clarify it for you. Link to comment Share on other sites More sharing options...
Recommended Posts